Home >Java >javaTutorial >Java code example to implement asynchronous call
The content of this article is about code examples for implementing asynchronous calls in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
First of all, the problem I encountered is that the cache needs to be updated when the interface is called, and updating the cache is a process that is fast or not, so I plan to make an asynchronous call to return the results I need. As for when the cache will be updated, It’s not what I need to pay attention to
Without further ado, let’s talk about the code
public class MyExecutor { private ExecutorService executor = Executors.newCachedThreadPool() ; public void fun() throws Exception { executor.submit(new Runnable(){ @override public void run() { try { //要执行的业务代码,我们这里没有写方法,可以让线程休息几秒进行测试 Thread.sleep(10000); System.out.print("睡够啦~"); }catch(Exception e) { throw new RuntimeException("报错啦!!"); } } }); } }
public class Demo{ public static void main(String[] args) { MyExecutor myExecutor = new MyExecutor(); try { myExecutor.fun(); System.our.print("你先睡着,我先回家啦~"); }catch(Exception e) { throw new RuntimeException("业务程序报错啦!!"); } } }
Okay, that’s the end of the code (ps: Please forgive me if there are any mistakes when typing by hand)
Run the main method
will print first (you fall asleep first, I will go home first~)
Then (sleep enough~)
That is to say, in When the method that requires asynchronous execution has not been completed, the main program has already returned the result. There is no need to continue waiting. This can ensure that the program returns the result first and then continues to perform tedious tasks that do not require waiting. Of course, you can also add some methods to determine whether the asynchronous method has been executed.
Let’s talk about the Executors class
This class is used to create a thread pool
There are several methods
1. newFixedThreadPool() creates a fixed size The size of the thread pool will remain unchanged once it reaches the maximum value. If a thread ends due to an execution exception, the thread pool will add a new thread
2. newCachedThreadPool() creates a cacheable Thread pool. If the size of the thread pool exceeds the threads required to process tasks, some idle threads (not executing tasks for 60 seconds) will be recycled. When the number of tasks increases, this thread pool can intelligently add new threads. Process tasks. This thread pool does not limit the size of the thread pool. The size of the thread pool completely depends on the maximum thread size that the system (JVM) can create
3. newSingleThreadExecutor() creates a single-threaded thread pool. This thread pool only has threads working, which is equivalent to a single thread executing all tasks serially. If the only thread ends abnormally, a new thread will replace it. This thread pool ensures that all tasks are executed in the order in which they are submitted
4. newScheduledThreadPool() creates a thread pool of unlimited size. This thread pool supports the need for timing and periodic execution of tasks
5. newSingleThreadScheduledExecutor() creates a single-threaded thread pool. This thread pool supports timing and periodic execution of tasks
The above is the detailed content of Java code example to implement asynchronous call. For more information, please follow other related articles on the PHP Chinese website!