Detailed explanation of Java executorservice usage

The following example mainly discusses two problems: problem 1 The thread pool has a fixed size, which is assumed to be 5 So what is the running effect of putting 10 threads into the thread pool? Status of other threads? Question 2 So how to remove a thread from the thread pool, specifically to make a thread idle?

example:

Results of running code directly:

Thread Name: --- threadname0 hash table size: 10 main thread sleep for a while! Thread Name: ---threadname2 thread Name: ---threadname4 thread Name: ---threadname1 thread Name: ---threadname3 thread Name: ---threadname4 thread Name: ---threadname2 thread Name: ---threadname3 thread Name: ---threadname1 thread Name: ---threadname0 thread Name: ---threadname1 thread Name: ---threadnam E3 thread Name: ---threadname0 thread Name: ---threadname4 thread Name: ---threadname2 thread Name: ---threadname1 thread Name: ---threadname3 thread Name: ---threadname4

Conclusion: it is found that the printed thread name has been from threadname0 to threadname4, and there is no other name. This proves that 10 threads are put into the thread pool, but the size of the thread pool is 5. Only 5 threads can be allocated CPU. The running is the first 5 threads put into the thread pool, and other threads are in the ready state (blocking state).

After removing the comments, the code runs as follows:

Thread Name: ------ threadname0 thread Name: ------ threadname2 thread Name: ------ threadname4 hash table size: 10 main thread sleep for a while! Thread Name: ------ threadname1 thread Name: ------ threadname3 delete thread thread0, hash table size: 9 delete thread thread1, hash table size: 8 delete thread thread2, hash table size: 7 ***********************************************************************************************, Thread Name: ********** threadname0 thread Name: ------ threadname5 thread Name: ------ threadname6 ************** thread end, Thread Name: ********** threadname1 thread Name: ---threadname4 thread Name: ---threadname7 thread Name: ---threadname3 thread Name: ---threadname6 thread Name: ---threadname5 thread Name: ---threadname7 thread Name: ---threadname4 thread Name: ---threadname3 thread Name: ---threadname5 thread Name: ---threa Dname6 thread Name: ---threadname7 thread Name: ---threadname4 thread Name: ---threadname3

Conclusion: it can be seen from the results that the running thread is still from thread0 to thread4 before removing the thread. When thread thread0 is removed, the new thread thread3 starts running and goes to threadname7 in order.

The summary is as follows:

@H_ 502_ 95@1. The thread pool has a fixed size, which is assumed to be 5 So what is the running effect of putting 10 threads into the thread pool? Status of other threads? a. The concept of thread pool is that you constantly push requests to it, but it can only handle the specified amount of threads, and redundant threads will wait in it. b. When one of the threads completes processing (business execution is completed or exiting the while loop), the thread pool will automatically take out a job from the waiting queue and use the idle thread to run the job. For example, which thread pool should be run in the order of being put in.

@H_ 502_ 95@2. So how to remove a thread from the thread pool, specifically to make a thread idle? The thread pool cannot obtain one of the threads and kill it, because the main thread using the thread pool and the thread opened by the main thread are of the same level, and no one has the right to dominate the survival of the other party. But you can change a way to achieve your goal tactfully. a. The main thread maintains a hash table, which can be a HashMap. The key value is arbitrary, but it should be unique. It can uniquely identify a thread. b. All threads put into the thread pool should generate a key value and then store it in the HashMap. c. For the thread of the loop class, such as the thread of while (true). A condition needs to be added to check whether the thread's key exists in the HashMap above in each round of loop. If it does not exist, exit the while loop. d. Although the main thread cannot dominate the survival of other threads, it can put or remove its own HashMap. At this point, as long as the key value corresponding to the thread is removed from the HashMap, the thread will automatically exit the next cycle.

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
分享
二维码
< <上一篇
下一篇>>