Use ThreadPoolExecutor

Look constructor, ThreadPoolExecutor total of four constructors:

 

 

 Look directly up to seven parameters parameter representing:

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,  TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)
  • corePoolSize : core thread pool threads
  • maximumPoolSize : The maximum number of thread pool
  • keepAliveTime : idle thread survival time
  • Unit : unit of time
  • workQueue : buffer queue thread pool used
  • threadFactory : create a thread pool thread factory used
  • Handler : thread pool strategy rejected tasks

1. When the number of threads in the pool are running (including the number of idle threads) less than corePoolSize, a new thread to perform tasks

 public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
        // 任务1
        pool.execute(() -> {
            try {
                Thread.sleep(3 * 1000);
                System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        //任务2
        pool.execute(() -> System.out.println("--helloWorld_002--" + Thread.currentThread().getName()));
}

Conclusion: After Thread 1 Thread 1 but did not continue to start the thread 2

2. When the number of threads in the pool is running (including idle threads) greater than or equal corePoolSize, newly inserted into the task queue workQueue (workQueue length if allowed), waiting for a free thread to execute.

    public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
        // 任务1
        pool.execute(() -> {
            try {
                Thread.sleep(3 * 1000);
                System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务2
        pool.execute(() -> {
            try {
                Thread.sleep(5 * 1000);
                System.out.println("--helloWorld_002--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务3
        pool.execute(() -> System.out.println("--helloWorld_003--" + Thread.currentThread().getName()));
}

 

 

 Conclusion: Task 2 during operation, task 3 will not start a new thread, because there is a queue is empty, maximumPoolSize = 3 This parameter has no effect.

3. When the task queue reaches the upper limit, and the ongoing thread pool is less than maxinumPoolSize, the task for new entrants, the new thread.

 public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
        // 任务1
        pool.execute(() -> {
            try {
                Thread.sleep(3 * 1000);
                System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务2
        pool.execute(() -> {
            try {
                Thread.sleep(5 * 1000);
                System.out.println("--helloWorld_002--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务3
        pool.execute(() -> System.out.println("--helloWorld_003--" + Thread.currentThread().getName()));
        // 任务4
        pool.execute(() -> System.out.println("--helloWorld_004--" + Thread.currentThread().getName()));
    }

Results: After the start task 1, task 3 in the queue, the queue on the full, since the number of threads is going 2 <maximumPoolSize, only a new task and a thread 4 on the new thread into -3, 4 task ends, the task queue 3 in the thread 3.

4. The task queue reaches the upper limit, and the thread pool is running equal maximumPoolSize, for adding new tasks, refused to execute the policy (the default thread pool strategy is to throw an exception).

public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1));
        // 任务1
        pool.execute(() -> {
            try {
                Thread.sleep(3 * 1000);
                System.out.println("--helloWorld_001--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务2
        pool.execute(() -> {
            try {
                Thread.sleep(5 * 1000);
                System.out.println("--helloWorld_002--" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        // 任务3
        pool.execute(() -> System.out.println("--helloWorld_003--" + Thread.currentThread().getName()));
        // 任务4
        pool.execute(() -> {
            try {
                Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("--helloWorld_004--" + Thread.currentThread().getName());
        });
        // 任务5
        pool.execute(() -> System.out.println("--helloWorld_005--" + Thread.currentThread().getName()));
    }
运行结果:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task ExecutorDemo$$Lambda$5/999966131@7699a589 rejected from java.util.concurrent.ThreadPoolExecutor@58372a00[Running, pool size = 3, active threads = 3, queued tasks = 1, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
    at ExecutorDemo.main(ExecutorDemo.java:40)
--helloWorld_004----pool-1-thread-3
--helloWorld_003--pool-1-thread-3
--helloWorld_001--pool-1-thread-1
--helloWorld_002--pool-1-thread-2

Conclusion: The queue reached the upper limit, the thread pool reaches the maximum value, it throws an exception.

Guess you like

Origin www.cnblogs.com/xcgShare/p/11754193.html