{"id":13694788,"url":"https://github.com/electronicarts/ea-async","last_synced_at":"2025-04-14T08:54:45.121Z","repository":{"id":37734368,"uuid":"48454315","full_name":"electronicarts/ea-async","owner":"electronicarts","description":"EA Async implements async-await methods in the JVM.","archived":false,"fork":false,"pushed_at":"2024-05-08T21:17:57.000Z","size":408,"stargazers_count":1398,"open_issues_count":23,"forks_count":137,"subscribers_count":48,"default_branch":"master","last_synced_at":"2025-04-07T01:09:37.590Z","etag":null,"topics":["async","async-await","asynchronous","concurrency","ea-async","java","jvm"],"latest_commit_sha":null,"homepage":"https://go.ea.com/ea-async","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/electronicarts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-22T21:16:08.000Z","updated_at":"2025-03-30T03:11:38.000Z","dependencies_parsed_at":"2024-10-13T03:51:27.927Z","dependency_job_id":null,"html_url":"https://github.com/electronicarts/ea-async","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/electronicarts%2Fea-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/electronicarts%2Fea-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/electronicarts%2Fea-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/electronicarts%2Fea-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/electronicarts","download_url":"https://codeload.github.com/electronicarts/ea-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248852109,"owners_count":21171839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","async-await","asynchronous","concurrency","ea-async","java","jvm"],"created_at":"2024-08-02T17:01:42.305Z","updated_at":"2025-04-14T08:54:45.099Z","avatar_url":"https://github.com/electronicarts.png","language":"Java","readme":"EA Async\n============\n\n[![Release](https://img.shields.io/github/release/electronicarts/ea-async.svg)](https://github.com/electronicarts/ea-async/releases)\n[![Maven Central](https://img.shields.io/maven-central/v/com.ea.async/ea-async-parent.svg)](http://repo1.maven.org/maven2/com/ea/async/)\n[![Javadocs](https://img.shields.io/maven-central/v/com.ea.async/ea-async.svg?label=Javadocs)](http://www.javadoc.io/doc/com.ea.async/ea-async)\n[![Build Status](https://img.shields.io/travis/electronicarts/ea-async.svg)](https://travis-ci.org/electronicarts/ea-async)\n\nEA Async implements Async-Await methods in the JVM.\nIt allows programmers to write asynchronous code in a sequential fashion.\n\nIt is heavily inspired by Async-Await on the .NET CLR, see [Asynchronous Programming with Async and Await](https://msdn.microsoft.com/en-us/library/hh191443.aspx) for more information.\n\nWho should use it?\n------\n\nEA Async should be used to write non-blocking asynchronous code that makes heavy use of CompletableFutures or CompletionStage.\nIt improves scalability by freeing worker threads while your code awaits other processes;\nAnd improves productivity by making asynchronous code simpler and more readable.\n\nDeveloper \u0026 License\n======\nThis project was developed by [Electronic Arts](http://www.ea.com) and is licensed under the [BSD 3-Clause License](LICENSE).\n\nExamples\n=======\n#### With EA Async\n\n```java\nimport static com.ea.async.Async.await;\nimport static java.util.concurrent.CompletableFuture.completedFuture;\n\npublic class Store\n{\n    public CompletableFuture\u003cBoolean\u003e buyItem(String itemTypeId, int cost)\n    {\n        if(!await(bank.decrement(cost))) {\n            return completedFuture(false);\n        }\n        await(inventory.giveItem(itemTypeId));\n        return completedFuture(true);\n    }\n}\n```\nIn this example `Bank.decrement` returns `CompletableFuture\u003cBoolean\u003e` and `Inventory.giveItem` returns `CompletableFuture\u003cString\u003e`\n\nEA Async rewrites the calls to `Async.await` making your methods non-blocking.\n\nThe methods look blocking but are actually transformed into asynchronous methods that use\nCompletableFutures to continue the execution as intermediary results arrive.\n\n#### Without EA Async\n\nThis is how the first example looks without EA Async. It is a bit less readable.\n\n```java\nimport static java.util.concurrent.CompletableFuture.completedFuture;\n\npublic class Store\n{\n    public CompletableFuture\u003cBoolean\u003e buyItem(String itemTypeId, int cost)\n    {\n        return bank.decrement(cost)\n            .thenCompose(result -\u003e {\n                if(!result) {\n                    return completedFuture(false);\n                }\n                return inventory.giveItem(itemTypeId).thenApply(res -\u003e true);\n            });\n    }\n}\n```\nThis is a small example... A method with a few more CompletableFutures can look very convoluted.\n\nEA Async abstracts away the complexity of the CompletableFutures.\n\n#### With EA Async (2)\n\nSo you like CompletableFutures?\nTry converting this method to use only CompletableFutures without ever blocking (so no joining):\n\n```java\nimport static com.ea.async.Async.await;\nimport static java.util.concurrent.CompletableFuture.completedFuture;\n\npublic class Store\n{\n    public CompletableFuture\u003cBoolean\u003e buyItem(String itemTypeId, int cost)\n    {\n        if(!await(bank.decrement(cost))) {\n            return completedFuture(false);\n        }\n        try {\n            await(inventory.giveItem(itemTypeId));\n            return completedFuture(true);\n        } catch (Exception ex) {\n            await(bank.refund(cost));\n            throw new AppException(ex);\n        }\n    }\n}\n```\n\nGot it? Send it [to us](https://github.com/electronicarts/ea-async/issues/new). It probably looks ugly...\n\nGetting started\n---------------\n\nEA Async currently supports JDK 8-10.\n\nIt works with Java and Scala and should work with most JVM languages.\nThe only requirement to use EA Async is that must be used only inside methods that return `CompletableFuture`, `CompletionStage`, or subclasses of `CompletableFuture`.\n\n### Using with maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.ea.async\u003c/groupId\u003e\n    \u003cartifactId\u003eea-async\u003c/artifactId\u003e\n    \u003cversion\u003e1.2.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Gradle\n\n```\n'com.ea.async:ea-async:1.2.3'\n```\n\n### Instrumenting your code\n\n#### Option 1 - JVM parameter\n\nStart your application with an extra JVM parameter: `-javaagent:ea-async-1.2.3.jar`\n```\n java -javaagent:ea-async-1.2.3.jar -cp your_claspath YourMainClass args...\n```\n\nIt's recommended to add this as a default option to launchers in IntelliJ projects that use ea-async.\n\n#### Option 2 - Runtime\nOn your main class or as early as possible, call at least once:\n```\nAsync.init();\n```\nProvided that your JVM has the capability enabled, this will start a runtime instrumentation agent.\nIf you forget to invoke this function, the first call to `await` will initialize the system (and print a warning).\n\nThis is a solution for testing and development, it has the least amount of configuration.\nIt might interfere with JVM debugging. This alternative is present as a fallback.\n\n#### Option 3 - Run instrumentation tool\n\nThe ea-async-1.2.3.jar is a runnable jar that can pre-instrument your files.\n\nUsage:\n\n```bash\njava -cp YOUR_PROJECT_CLASSPATH -jar ea-async-1.2.3.jar classDirectory\n```\n\nExample:\n\n```bash\njava -cp guava.jar;commons-lang.jar  -jar ea-async-1.2.3.jar target/classes\n```\n\nAfter that all the files in target/classes will have been instrumented.\nThere will be no references to `Async.await` and `Async.init` left in those classes.\n\n\n#### Option 4 - Build time instrumentation, with Maven - Preferred\n\nUse the [ea-async-maven-plugin](maven-plugin). It will instrument your classes in compile time and\nremove all references to `Async.await` and `Async.init()`.\n\nWith build time instrumentation your project users won't need to have EA Async in their classpath unless they also choose to use it.\nThis means that EA Async \u003ci\u003edoes not need to be a transitive dependency\u003c/i\u003e.\n\nThis is the best option for libraries and maven projects.\n\n```xml\n\u003cbuild\u003e\n    \u003cplugins\u003e\n        \u003cplugin\u003e\n            \u003cgroupId\u003ecom.ea.async\u003c/groupId\u003e\n            \u003cartifactId\u003eea-async-maven-plugin\u003c/artifactId\u003e\n            \u003cversion\u003e1.2.3\u003c/version\u003e\n            \u003cexecutions\u003e\n                \u003cexecution\u003e\n                    \u003cgoals\u003e\n                        \u003cgoal\u003einstrument\u003c/goal\u003e\n                        \u003cgoal\u003einstrument-test\u003c/goal\u003e\n                    \u003c/goals\u003e\n                \u003c/execution\u003e\n            \u003c/executions\u003e\n        \u003c/plugin\u003e\n    \u003c/plugins\u003e\n\u003c/build\u003e\n```\n\n","funding_links":[],"categories":["Java","并发编程"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felectronicarts%2Fea-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felectronicarts%2Fea-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felectronicarts%2Fea-async/lists"}