//This may be the only newCachedThreadPool used in a project, the others are the size of a new fixed public static ExecutorService exec = Executors.newCachedThreadPool(); private static List<String> userList; static { userList=new ArrayList<String>(); for (int i = 0; i < 100; i++){ userList.add(String.valueOf(i)); } } /** * Use semaphore to control the number of threads */ public static void simpleSemaphore() { // Build maximum thread concurrency final Semaphore sem = new Semaphore (10); final Semaphore sycSem = new Semaphore(5, true);//Non-competitive locks are used here //Simulate 100 customer visits for (final String user:userList) { Runnable run = new Runnable() { public void run() { try { sem.acquire();//Get permission System.out.println(user + "User enters----"); sem.release();//Release access System.out.println("There are " + sem.availablePermits() + " thread spaces available after access"); } catch (InterruptedException e) { e.printStackTrace (); } } }; exec.execute(run); } exec.shutdown(); }
Results of the
0 users enter ---- There are still 10 thread spaces available after access 1 User enters ---- There are still 9 thread spaces that can be used after access 16 users enter ---- There are still 9 thread spaces that can be used after access 6 users enter ---- There are still 9 thread spaces that can be used after access 19 users enter ---- There are still 9 thread spaces that can be used after access 23 users enter---- There are still 9 thread spaces that can be used after access 26 users enter ----
What should I do if the users above are replaced by thread pools?
//This may be the only newCachedThreadPool used in a project, the others are the size of a new fixed public static ExecutorService exec = Executors.newCachedThreadPool(); private static BlockingQueue<TaskPool> taskPools = new ArrayBlockingQueue<TaskPool>(1000); private String user; public static void addPoolTask(TaskPool user) { if (taskPools.size() < 1000) {//It is recommended to add an initialization value taskPools.add(user); } else { // transfer or do other processing } } /** * Use semaphore to control the number of threads */ public static void simpleSemaphore() { // Build maximum thread concurrency final Semaphore sem = new Semaphore (10); final Semaphore sycSem = new Semaphore(5, true);//Non-competitive locks are used here //Simulate 100 customer visits while (taskPools.size() > 0) { try { final TaskPool taskPool = taskPools.take(); Runnable run = new Runnable() { public void run() { try { sem.acquire();//Get permission //Execute the corresponding task pool thing here taskPool.excute(); sem.release();//Release access System.out.println("thread pool" + taskPool.toString() + "after access" + sem.availablePermits() + "thread space can be used"); } catch (InterruptedException e) { e.printStackTrace (); } } }; exec.execute(run); } catch (InterruptedException e) { e.printStackTrace (); } } exec.shutdown(); }
Multi-thread pool management based on user expansion
public interface TaskPool { void excute(); } public class MyFirstPoolTask implements TaskPool { public ExecutorService exec = Executors.newFixedThreadPool(1); private int init; private int max = 10; public MyFirstPoolTask(int init) { if (max < init) System.out.println("The value is too large"); this.exec = Executors.newFixedThreadPool(init); } public void excute() { for (int i = 0; i < 5; i++) { final int z = i; Runnable rn = new Runnable() { public void run() { System.out.println("MyFirstPoolTask does my business processing"+Thread.currentThread().getName()); } }; exec.execute(rn); } exec.shutdown(); } } public class MySecondPoolTask implements TaskPool { public ExecutorService exec = Executors.newFixedThreadPool(1); private int init; private int max = 10; public MySecondPoolTask(int init) { if (max < init) System.out.println("The value is too large"); this.exec = Executors.newFixedThreadPool(init); } public void excute() { for (int i = 0; i < 5; i++) { final int z = i; Runnable rn = new Runnable() { public void run() { System.out.println("MySecondPoolTask does my business processing"+Thread.currentThread().getName()); } }; exec.execute(rn); } exec.shutdown(); } }
result after execution
The thread pool test.com.jd.hotel.janleTest.MyFirstPoolTask@2678a212 still has 9 thread spaces to use after accessing MyFirstPoolTask does my business processing pool-3-thread-3 MyFirstPoolTask does my business processing pool-3-thread-3 MyFirstPoolTask does my business processing pool-3-thread-3 The thread pool test.com.jd.hotel.janleTest.MySecondPoolTask@151a64ed still has 10 thread spaces to use after accessing MySecondPoolTask for my business processing pool-5-thread-1 MySecondPoolTask for my business processing pool-5-thread-1 MyFirstPoolTask does my business processing pool-3-thread-1 MySecondPoolTask for my business processing pool-5-thread-4 MySecondPoolTask for my business processing pool-5-thread-2 MyFirstPoolTask for my business processing pool-3-thread-2 MySecondPoolTask for my business processing pool-5-thread-3