The following examples mainly discuss two issues:
Question 1. The thread pool is fixed in size, assuming it is 5. Then how is the operation effect of putting 10 threads into the thread pool? The status of other threads?
Question 2. So how do you remove a thread from the thread pool, or to be precise, to make a thread an idle thread?
example:
package ; import ; public class JobThread extends Thread { // Name the thread public JobThread(String name,long threadId) { super(name); } @Override public void run() { // If the main thread contains this thread, it will run while ((())) { try { ("Thread Name: -----" + ()); (4); } catch (Exception e) { (); } } ("***************Thread ends, thread name: **********" + ()); } }
package ; import ; import ; import ; import ; import ; public class MainThread { public static final int THREADPOOL_SIZE = 5; // Generate a fixed-size thread pool public static ExecutorService exec = (THREADPOOL_SIZE); // Map used to store thread names public static Map<String, String> threadNameMap = new HashMap<String, String>(); public static void main(String[] args) { // Insert 10 threads into the thread pool, but the thread pool only allows a maximum of 5 threads, so the other 5 threads are waiting for (int i = 0; i < THREADPOOL_SIZE + 5; i++) { String threadName = getThreadName(i); (threadName, threadName); (new JobThread(threadName, i)); } ("Size of Hash table:" + ()); try { ("Sleep for a while on the main thread!"); (3); } catch (Exception e) { (); ("woke up!"); } // The following are used to delete threads in the thread pool //removeThread(0); //removeThread(1); //removeThread(2); } public static void removeThread(int i) { (getThreadName(i)); ("Delete Thread" + i + ", Size of the Hash table:" + ()); } public static String getThreadName(int i) { return "threadname"+i; } }
Directly run the code result:
Threadname: ------threadname0
The size of the hash table: 10
Sleep for a while on the main thread!
Threadname: ------threadname2
Threadname: ------threadname4
Threadname: ------threadname1
Threadname: ------threadname3
Threadname: ------threadname4
Threadname: ------threadname2
Threadname: ------threadname3
Threadname: ------threadname1
Threadname: ------threadname0
Threadname: ------threadname1
Threadname: ------threadname3
Threadname: ------threadname0
Threadname: ------threadname4
Threadname: ------threadname2
Threadname: ------threadname1
Threadname: ------threadname3
Threadname: ------threadname4
in conclusion:
Found printed: the thread name has been from threadname0 to threadname4, no other name.
It is proved that 10 threads are placed into the thread pool, but the size of the thread pool is 5, and only 5 threads can be allocated CPUs. The running one is the first to put into the thread pool, and the other threads are in the ready state (blocking state).
After removing the comments, the code runs:
Threadname: ------threadname0
Threadname: ------threadname2
Threadname: ------threadname4
The size of the hash table: 10
Sleep for a while on the main thread!
Threadname: ------threadname1
Threadname: ------threadname3
Delete thread Thread0, the size of the Hash table: 9
Delete thread Thread1, the size of the hash table: 8
Delete thread Thread2, the size of the hash table: 7
***************Thread ends, thread name: *********threadname2
***************Thread ends, thread name: *********threadname0
Threadname: ------threadname5
Threadname: ------threadname6
***************Thread ends, thread name: *********threadname1
Threadname: ------threadname4
Threadname: ------threadname7
Threadname: ------threadname3
Threadname: ------threadname6
Threadname: ------threadname5
Threadname: ------threadname7
Threadname: ------threadname4
Threadname: ------threadname3
Threadname: ------threadname5
Threadname: ------threadname6
Threadname: ------threadname7
Threadname: ------threadname4
Threadname: ------threadname3
in conclusion:
From the results, we can see that before removing the thread, the running thread is still from thread0 to thread4. After removing thread thread 0, the new thread thread3 starts running, and it goes to threadname7 in order.
The summary is as follows:
1. The thread pool is fixed in size, assuming it is 5. Then how is the operation effect of putting 10 threads into the thread pool? The status of other threads?
a. The concept of thread pool is that you keep pushing requests to it, but it can only handle threads with specified quotas, and extra threads will wait in it.
b. When one of the threads has completed processing (the business execution is completed or the while loop is exited), the thread pool will automatically take out a job from the waiting queue and use an idle thread to run the job. Which thread pool in the running thread pool should be based on the order in which it is placed.
2. So how to remove a thread from the thread pool, or to be precise, to make a thread an idle thread?
The thread pool cannot obtain one of the threads and kill it because the main thread using the thread pool and the threads opened by the main thread are on the same level, and no one has the right to dominate the survival of the other party. But you can change the method and achieve the goal in a tactful way.
a. The main thread maintains a Hash table that can be a HashMap. The key value is arbitrary, but it must be unique and can uniquely indicate a thread.
b. All threads placed in the thread pool must generate a key value and then store it in this HashMap.
c. For threads of loop class, such as threads of while(true). A condition needs to be added to verify whether the key of this thread exists in the above HashMap for each round of loop. Exit the while loop if it does not exist.
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 corresponding key value of the thread is removed from the HashMap, the thread will automatically exit the next time it loops.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.