The following example mainly discusses two issues:
Question 1: Thread pool fixed size, assumed to be 5. Then put 10 threads into the thread pool, how does it work? State of other threads?
Question 2. So how do you remove a thread from the thread pool, or rather make a thread an idle thread?
Example:
Package com.dada.executorService;
Import Java.util.concurrent.TimeUnit;
public class Jobthread extends thread {
//thread naming public
jobthread (String name,long threadId) {
super (name); c6/>}
@Override public
void Run () {
//If the primary thread contains this thread, it runs while
( MainThread.threadNameMap.containsKey (This.getname ())) {
try {
System.out.println ("Thread Name:-----" + This.getname ());
TimeUnit.SECONDS.sleep (4);
} catch (Exception e) {
e.printstacktrace ();
}
}
System.out.println ("*************** thread end, thread name: *********" + this.getname ());
}
Package com.dada.executorService;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.TimeUnit;
public class Mainthread {public static final int threadpool_size = 5;
Generate a fixed size thread pool public static Executorservice exec = Executors.newfixedthreadpool (threadpool_size);
The Map public static map<string used to store the thread name, string> threadnamemap = new hashmap<string, string> (); public static void Main (string[] args) {//Insert 10 threads into the thread pool, but the thread pool allows only a maximum of 5 threads, so the other 5 threads wait for (int i = 0; i < THR Eadpool_size + 5;
i++) {String threadname = getthreadname (i);
Threadnamemap.put (ThreadName, threadname);
Exec.execute (New Jobthread (ThreadName, i));
} System.out.println ("Size of the hash table:" + threadnamemap.size ()); try {System.out.println ("main thread") sleep for a while!
");
TimeUnit.SECONDS.sleep (3);
catch (Exception e) {e.printstacktrace (); SysTem.out.println ("Wake Up!")
");
The following are several threads//removethread (0) used to delete the thread pool inside;
Removethread (1);
Removethread (2);
public static void Removethread (int i) {threadnamemap.remove (Getthreadname (i));
SYSTEM.OUT.PRINTLN ("Thread thread" + i + ", size of the hash table:" + threadnamemap.size ());
public static String getthreadname (int i) {return "ThreadName" +i; }
}
Run code results directly:
Thread Name:-----THREADNAME0
Size:10 of hash table
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:-----threadname3
Thread Name:-----THREADNAME0
Thread Name:-----threadname4
Thread Name:-----threadname2
Thread Name:-----threadname1
Thread Name:-----threadname3
Thread Name:-----threadname4
Conclusion:
found printed: Thread name always from THREADNAME0 to Threadname4, no other name.
This proves that 10 threads are placed into the thread pool, but the thread pool size is 5, and only 5 threads are allocated a CPU, which runs the first 5 threads into the thread pool, and the other threads are in a ready state (blocking state).
After you remove the annotation, the code runs the result:
Thread Name:-----THREADNAME0
Thread Name:-----threadname2
Thread Name:-----threadname4
Size:10 of hash table
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 end, thread name: *********threadname2
Thread end, 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:-----threadname6
Thread Name:-----threadname7
Thread Name:-----threadname4
Thread Name:-----threadname3
Conclusion:
as you can see from the results, the running thread is still from thread0 to THREAD4 before the thread is removed. When the thread thread0 is removed, the new thread thread3 begins to run, and in order to Threadname7.
Summarized as follows:
1. Thread pool fixed size, assumed to be 5. Then put 10 threads into the thread pool, how does it work? State of other threads?
A. The concept of a thread pool is that you constantly push the request to it, but it can only handle the specified amount of threads, and the extra threads will wait in it.
B. When one of the threads finishes (the business completes or exits the while loop), the thread pool automatically pulls a job out of the waiting queue and runs the job with idle threads. Which of the running thread pools, in the example, should be in the order in which they were put.
2. So how do you remove a thread from the thread pool, or rather make a thread an idle thread?
The thread pool could not fetch one of the threads and kill him, because the thread that was opened by the main thread and main thread of the thread pool is a peer, and no one has the right to dominate the other's survival. But it can be changed in a way that is tactful to achieve the goal.
A. Main thread maintenance a hash table can be a hashmap. The key value is arbitrary, but unique, can uniquely mark a thread.
B. All threads that are placed in the thread pool are generated with a key value and then stored in this hashmap.
C. For threads that loop the class, such as while (true). You need to add a condition, each round loop verifies whether the key of this thread exists in the hashmap above. Exits the while loop if it does not exist.
D. Although the main thread can not dominate the survival of other threads, but can be put on their own hashmap or remove operation. Here, as long as the thread's key value is removed from the HashMap, the thread automatically exits the next time it loops.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.