In this chapter we will discuss the thread pool of executor.
1. What is a thread pool? (The explanations below are from http://baike.haosou.com/doc/2511076-2653416.html)
The thread pool is a form of multithreading that adds tasks to the queue during processing, and then automatically starts those tasks after the thread is created. Thread pool threads are all background threads. Each thread uses the default stack size, runs at the default priority, and is in a multithreaded apartment. If a thread is idle in managed code, such as waiting for an event, the thread pool inserts another worker thread to keep all the processors busy. If all thread pool threads are always busy, but the queue contains pending work, the thread pool will create another worker thread after a period of time but the number of threads will never exceed the maximum value. Threads that exceed the maximum value can be queued, but they will not start until other threads have finished.
2. Function:
(1) centralized management of threads
(2) Increase the utilization of threads, because the application will start multiple threads, and these threads may be dormant or waiting for most of the time, we need to use them.
3. Example:
(1) The system automatically manages the number of threads
Package Com.ray.ch17;import Java.util.random;import Java.util.concurrent.callable;import Java.util.concurrent.executionexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.future;public class Test {public static void main (string[] args) throws Interruptedexception,executionexception {Executorservice executors = Executors.newcachedthreadpool (); callable<integer> callable = new Mycall (); future<integer> future = Executors.submit (callable); System.out.println (Future.get ()); Executors.shutdown ();}} Class Mycall implements callable<integer> {@Overridepublic Integer call () throws Exception {System.out.println (" Doing something ... "); Thread.Sleep (+); return new Random (). Nextint (50);}}
Output:
Doing something ...
17
(2) Manually set the number of threads
Package Com.ray.ch17;import Java.util.random;import Java.util.concurrent.callable;import Java.util.concurrent.executionexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.future;public class Test {public static void main (string[] args) throws Interruptedexception,executionexception {Executorservice executors = Executors.newfixedthreadpool (2); callable<integer> callable = new Mycall (); future<integer> future = Executors.submit (callable); System.out.println (Future.get ()); Executors.shutdown ();}} Class Mycall implements callable<integer> {@Overridepublic Integer call () throws Exception {System.out.println (" Doing something ... "); Thread.Sleep (+); return new Random (). Nextint (50);}}
Output:
Doing something ...
17
4. Note the point:
(1) When creating a thread pool, we can determine the number of threads depending on the actual situation or the system's decision.
(2) Submit runnable or callable tasks using the Submit method
(3) If you want to cancel a task or submit a callable task, you need to save the object returned by the future
(4) Finally remember to add the Shundown method to close the thread pool.
Summary: This chapter focuses on the thread pool of executor.
This chapter is here, thank you.
-----------------------------------
Directory
Know java-17.7 Actuator (Executor) from the beginning