自定义线程池

x33g5p2x  于2022-05-11 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(367)

一 线程类

package concurrent.policy;

/**
* @className: MyThread
* @description: 线程类
* @date: 2022/5/8
* @author: cakin
*/
public class MyThread implements Runnable {
    private String threadName;
    public MyThread(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        try {
            System.out.println("threadName :" + this.threadName);
            System.out.println(threadName + "执行了 1 秒钟...");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getThreadName() {
        return threadName;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }
}

二 线程池测试类

package concurrent.policy;

import java.util.concurrent.*;

/**
* @className: TestMyThreadPool
* @description: 线程池测试类
* @date: 2022/5/8
* @author: cakin
*/
public class TestMyThreadPool {
    public static void main(String[] args) {
      /*
         核心线程:1,如果只有1个任务,会直接交给线程池中的这一个线程来处理
         最大线程数:2,如果任务的数量>核心线程数1+workQueue.size(),且任务的数量<=大线程数2+workQueue.size()之和时,就将新提交的任务交给非核心线程处理
         最大空闲时间:10秒
         任务队列:有界队列ArrayBlockingQueue,该队列中可以存放3个任务
         拒绝策略:AbortPolicy(),    当提交任务数>最大线程数2+workQueue.size()之和时,任务会交给AbortPolicy()来处理
       */
        ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 2, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(3),
                new ThreadPoolExecutor.AbortPolicy()
        );
        MyThread t1 = new MyThread("t1");
        MyThread t2 = new MyThread("t2");
        MyThread t3 = new MyThread("t3");
        MyThread t4 = new MyThread("t4");
        MyThread t5 = new MyThread("t5");
        MyThread t6 = new MyThread("t6");
        pool.execute(t1);
/*        pool.execute(t2);
        pool.execute(t3);
        pool.execute(t4);
        pool.execute(t5);
        pool.execute(t6);*/
        pool.shutdown();
    }
}

三 测试说明

1 只让 t1 执行

threadName :t1

t1执行了 1 秒钟...

线程池中正好有1个核心线程能够处理,因此会直接运行。

2 让 t1 和 t2 执行

threadName :t1

t1执行了 1 秒钟...

threadName :t2

t2执行了 1 秒钟...

此时提交了2个任务,但线程池中仅有1个核心线程能够处理一个任务,那么另一个任务会被放入 ArrayBlockingQueue 中等待执行。t1 在执行完一秒后,t2 才会得到运行。

3 让 t1、t2、t3  执行

threadName :t1

t1执行了 1 秒钟...

threadName :t2

t2执行了 1 秒钟...

threadName :t3

t3执行了 1 秒钟...

因为 ArrayBlockingQueue 中能同时容纳 3 个任务,因此此时的执行逻辑和第 2 种情况一样。

4 让 t1、t2、t3、t4  执行

threadName :t1

t1执行了 1 秒钟...

threadName :t2

t2执行了 1 秒钟...

threadName :t3

t3执行了 1 秒钟...

threadName :t4

t4执行了 1 秒钟...

因为 ArrayBlockingQueue 中能同时容纳 3 个任务,因此此时的执行逻辑和第 2 种情况一样。

5 让 t1、t2、t3、t4、t5 执行

threadName :t5

t5执行了 1 秒钟...

threadName :t1

t1执行了 1 秒钟...

threadName :t2

t2执行了 1 秒钟...

threadName :t3

t3执行了 1 秒钟...

threadName :t4

t4执行了 1 秒钟...

t1 线程被核心线程执行,t2到t4被放到 ArrayBlockingQueue 等待执行,t5 被非核心线程执行。

6 让 t1、t2、t3、t4、t5、t6  执行

threadName :t5

threadName :t1

t5执行了 1 秒钟...

t1执行了 1 秒钟...

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task concurrent.policy.MyThread@372f7a8d rejected from java.util.concurrent.ThreadPoolExecutor@2f92e0f4[Running, pool size = 2, active threads = 2, queued tasks = 3, 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 concurrent.policy.TestMyThreadPool.main(TestMyThreadPool.java:37)

threadName :t2

threadName :t3

t3执行了 1 秒钟...

t2执行了 1 秒钟...

threadName :t4

t4执行了 1 秒钟...

t1 线程被核心线程执行,t2到t4被放到 ArrayBlockingQueue 等待执行,t5 被非核心线程执行。t6 因为饱和被拒绝策略 AbortPolicy 拒绝提交。

相关文章