Understand the use of executorservice in Java multithreading

java. util. The concurrent package provides classes for multithreading operations. Executorservice and its implementation classes (such as ThreadPoolExecutor), executor, executors, future, callable, etc. are commonly used

1. Executorservice (inherited from executor) interface: it provides some asynchronous multi-threaded operation methods, such as execute(), submit(), shutdown(), shutdown now(), etc

2. Executor interface: execute the submitted task (thread), and there is only one method execute (runnable a)

2. Executors class: provides factory methods and public methods to operate executor subclasses and threadfactory, such as newxxx(), xxthreadfactory()

3. Futrue interface: it represents the thread execution result and provides methods to obtain the thread execution result and cancel the thread, such as get(), cancle(), etc

4. Callable interface: jdk1 The thread with return value provided by 5 executes the new interface

Make a simple record of the understanding of executorservice and future

code:

Output:

The console prints the above output after waiting for 5 seconds. The reason is that when all threads start, they will wait for 5 seconds. Therefore, on the whole, they only wait for 5 seconds. This is a concurrent operation

Summary:

1. The difference between execute() method and submit() method provided by executorservice:

A. the execute () method only accepts instances of runnable type, so it cannot get the return value or dynamically get the thread execution

B. the submit () method accepts runnable and callable instances and returns the future instance. The get () method of the future instance can obtain the thread execution return value and throw thread execution exceptions. Therefore, you can use the submit () method if you want to get the results returned by the thread execution and handle the possible exceptions during the thread execution, or if you want to cancel the thread execution halfway

2. Through the output, you can see that the main method (main thread) ends after the execution of all threads. The reason is:

A. obtain the future instance through the submit() method and the thread return result through the get() method of the future instance. The get() method of the future instance will wait for the thread to finish executing before returning. Therefore, the main method will wait for all sub threads to finish

B. if the for loop marked in red above is removed, the main method (main thread) will end in advance without waiting for all child threads to end

Supplement:

1. When multiple threads execute concurrently, if one thread has an exception and is not handled, the thread will automatically stop execution, but other threads will still execute normally. This is why Tomcat can continue to provide services when an exception occurs in a Tomcat request.

2. Tomcat provides thread pool and waiting pool. Each request will restart a new thread to process the request. If the thread in the thread pool runs out, the request will be placed in the waiting pool to wait. When a thread is released back into the thread pool, thread processing requests will be allocated to the requests in the waiting pool.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>