栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自定义线程池

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义线程池

一 线程类
package concurrent.policy;


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.*;


public class TestMyThreadPool {
    public static void main(String[] args) {
      
        ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 2, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue(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.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 拒绝提交。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/868093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号