java ExecutorService method of use

  • 2020-05-19 04:45:38
  • OfStack

The following examples focus on two issues:
Question 1. Fixed thread pool size, let's say 5. How does it work when 10 threads are put into the thread pool? State of other threads?
Question 2. So how do you remove a thread from the thread pool, specifically making a thread idle?

Example:


package com.dada.executorService; 
 
import java.util.concurrent.TimeUnit; 
 
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 1 Straight run  
  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 ends, 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; 
 //  Generates a fixed-size thread pool  
 public static ExecutorService exec = Executors.newFixedThreadPool(THREADPOOL_SIZE); 
 //  Used to store thread names map 
 public static Map<String, String> threadNameMap = new HashMap<String, String>(); 
  
 public static void main(String[] args) { 
  //  Inserts into the thread pool  10  Four threads, but the thread pool is only allowed to the maximum  5  Threads, so the others  5  Three threads are waiting  
  for (int i = 0; i < THREADPOOL_SIZE + 5; i++) { 
   String threadName = getThreadName(i); 
   threadNameMap.put(threadName, threadName); 
   exec.execute(new JobThread(threadName, i)); 
  } 
   
  System.out.println("Hash The table Size : " + threadNameMap.size()); 
   
  try { 
   System.out.println(" The main thread to sleep 1 Will! "); 
   TimeUnit.SECONDS.sleep(3); 
  } catch (Exception e) { 
   e.printStackTrace(); 
   System.out.println(" Wake up! "); 
  } 
   
    //  The following are used to delete threads from the thread pool  
  //removeThread(0); 
  //removeThread(1); 
  //removeThread(2); 
 } 
  
 public static void removeThread(int i) { 
  threadNameMap.remove(getThreadName(i)); 
  System.out.println(" Delete the thread Thread" + i + ", Hash The table Size : " + threadNameMap.size()); 
 } 
  
 public static String getThreadName(int i) { 
  return "threadname"+i; 
 } 
  
} 

Direct running code results:

Thread name: --threadname0
Size: 10 in the Hash table
Main thread sleep 1 will!
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 1 straight from threadname0 to threadname4, no other name.
Proof: put 10 threads into the thread pool, but the thread pool size is 5, only 5 threads can be allocated CPU, running the first 5 threads into the thread pool, other threads are in the ready state (block state).

After the comments are removed, the code runs:

Thread name: --threadname0
Thread name: --threadname2
Thread name: --threadname4
Size: 10 for Hash table
Main thread sleep 1 will!
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 ends, thread name: *******threadname2
*************** thread ends, thread name: ********threadname0
Thread name: --threadname5
Thread name: --threadname6
************** thread ends, 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 threads were still running from thread0 to thread4 before they were removed. When thread thread0 is removed, the new thread thread3 starts running, in order to threadname7.

The summary is as follows:

1. Fixed thread pool size, let's say 5. Then put 10 threads into the thread pool, what is the running effect? State of other threads?
a. The idea of a thread pool is that you keep asking for push, but it can only handle a certain number of threads, and all the extra threads will be waiting in it.
b. When one of the threads finishes processing (business execution or exit the while loop), the thread pool automatically pulls a job out of the waiting queue and runs the job using the idle thread. Which of the thread pools should be run, as an example, in the order in which they are put.

2. So how do you remove a thread from the thread pool, specifically making a thread idle?
The thread pool cannot take one of these threads and kill it, because the main thread that USES the thread pool and the thread that the main thread started are parallel, and no one has the right to dominate the other. But can change 1 way, achieve the goal tactfully.
a. Main thread maintains 1 Hash table which can be 1 HashMap. The key value is arbitrary, but to be unique to 1, you can flag 1 thread at 1.
b. All threads that are put into the thread pool generate an key value and store it in this HashMap.
c. For the thread of the loop class, such as the thread of while(true). You need to add a condition to verify that the key of this thread exists in HashMap above every round of the loop. Exit the while loop if it does not exist.
d. Although the main thread can not dominate the survival of other threads, it can perform put or remove operations on its own HashMap. At this point, simply remove the Key value of the thread from HashMap, and the thread will automatically exit the next time it loops.


Related articles: