In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the method of Java asynchronous call". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of Java asynchronous call".
Create thread @ Testpublic void test0 () throws Exception {System.out.println ("main function starts execution"); Thread thread=new Thread (new Runnable () {@ Override public void run () {System.out.println ("= task start==="); try {Thread.sleep (5000);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("= task finish===");}}) Thread.start (); System.out.println ("end of main function execution");} II. Future
In the previous implementation of jdk8, Future is added under JUC, which literally means the future, but it is a bit creepy to use, and it can not achieve asynchronism in the real sense. You need to block threads or keep polling when getting results.
@ Testpublic void test1 () throws Exception {System.out.println ("main function starts execution"); ExecutorService executor = Executors.newFixedThreadPool (1); Future future = executor.submit (new Callable () {@ Override public Integer call () throws Exception {System.out.println ("= task start==="); Thread.sleep (5000); System.out.println ("= task finish==="); return 3 }}); / / the main thread will be blocked when the return value is required. If the return value is not needed, it will be OK. Can also receive / / Integer result=future.get (); System.out.println ("end of main function execution"); System.in.read ();} III. CompletableFuture
Using native CompletableFuture to implement asynchronous operations, coupled with support for lambda, it can be said that the implementation of asynchronous tasks has been carried out to the extreme.
@ Testpublic void test2 () throws Exception {System.out.println ("main function starts to execute"); ExecutorService executor = Executors.newFixedThreadPool (2); CompletableFuture future = CompletableFuture.supplyAsync (new Supplier () {@ Override public Integer get () {System.out.println ("= task start==="); try {Thread.sleep (5000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("= task finish==="); return 3;}}, executor); future.thenAccept (e-> System.out.println (e)); System.out.println ("end of main function execution");} IV. Async comments of Spring
To implement asynchrony with spring, you need to enable annotations. You can use xml or Java config.
Xml mode: "executor" / > "executor" pool-size= "2" thread pool size queue-capacity= "100" queue length keep-alive=" 120thread keep alive time (in sec) rejection-policy= "CALLER_RUNS" processing policy for rejected tasks / > java mode: @ EnableAsyncpublic class MyConfig {@ Bean public TaskExecutor executor () {ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor (); executor.setCorePoolSize (10) / / number of core threads executor.setMaxPoolSize (20); / / maximum number of threads executor.setQueueCapacity (1000); / / queue size executor.setKeepAliveSeconds (300); / / maximum idle time of threads executor.setThreadNamePrefix ("fsx-Executor-"); / / specify the prefix for the newly created thread name. Executor.setRejectedExecutionHandler (new ThreadPoolExecutor.CallerRunsPolicy ()); return executor;}} (1) @ Async@Testpublic void test3 () throws Exception {System.out.println ("main function starts to execute"); myService.longtime (); System.out.println ("main function execution ends");} @ Asyncpublic void longtime () {System.out.println ("I'm performing a time-consuming task"); try {Thread.sleep (5000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println (complete);} (2) AsyncResult
If a return value is required, the time-consuming method return value is wrapped in AsyncResult.
@ Testpublic void test4 () throws Exception {System.out.println ("main function starts to execute"); Future future=myService.longtime2 (); System.out.println ("main function execution ends"); System.out.println ("Asynchronous execution result: + future.get ());} @ Asyncpublic Future longtime2 () {System.out.println (" I'm performing a time-consuming task "); try {Thread.sleep (8000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("complete"); return new AsyncResult (3);} Thank you for your reading. The above is the content of "what is the method of Java asynchronous call". After the study of this article, I believe you have a deeper understanding of what the method of Java asynchronous call is, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.