JAVA并发,线程工厂及自定义线程池

本文介绍如何通过编写定制的ThreadFactory来创建具有特定属性(如后台线程)的线程,并实现了一个自定义线程池执行器。通过实例演示了如何在Java中使用这些自定义组件来执行后台任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 package com.xt.thinks21_2;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.SynchronousQueue;
 6 import java.util.concurrent.ThreadFactory;
 7 import java.util.concurrent.ThreadPoolExecutor;
 8 import java.util.concurrent.TimeUnit;
 9 
10 /**
11  * 后台线程工厂测试
12  * 
13  * @step 1
14  * @author Administrator
15  *
16  */
17 class DaemonThreadFactory implements ThreadFactory {
18 
19     @Override
20     public Thread newThread(Runnable r) {
21         // TODO Auto-generated method stub
22         Thread t = new Thread(r);
23         t.setDaemon(true);
24         return t;
25     }
26 
27 }
28 
29 /**
30  * 自定义线程池执行器
31  * 
32  * @step 5
33  * @author Administrator
34  *
35  */
36 class DaemonThreadPoolExecutor extends ThreadPoolExecutor {
37 
38     public DaemonThreadPoolExecutor() {
39         super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
40                 new SynchronousQueue<Runnable>(), new DaemonThreadFactory());
41         // TODO Auto-generated constructor stub
42     }
43 
44 }
45 
46 /**
47  * 后台线程测试
48  * 
49  * @step 2
50  * @author Administrator
51  *
52  */
53 public class DaemonFromFactory implements Runnable {
54     // @step 3
55     @Override
56     public void run() {
57         // TODO Auto-generated method stub
58         while (true) {
59             try {
60                 TimeUnit.MILLISECONDS.sleep(100);
61                 System.out.println(Thread.currentThread() + ":" + this);
62             } catch (InterruptedException e) {
63                 // TODO Auto-generated catch block
64                 e.printStackTrace();
65             }
66         }
67     }
68 
69     public static void main(String[] args) {
70         // @step 4
71         ExecutorService es = Executors
72                 .newCachedThreadPool(new DaemonThreadFactory());
73         for (int i = 0; i < 10; i++) {
74             es.execute(new DaemonFromFactory());// 执行后台线程
75         }
76         es.shutdown();
77         // @step 6
78         // 自定义的线程池执行器
79         DaemonThreadPoolExecutor dte = new DaemonThreadPoolExecutor();
80         for (int i = 0; i < 10; i++) {
81             dte.execute(new DaemonFromFactory());// 执行后台线程
82         }
83         dte.shutdown();
84         System.out.println("ALL DEAMON THREAD IS START!");
85         try {
86             TimeUnit.MILLISECONDS.sleep(125);
87         } catch (InterruptedException e) {
88             // TODO Auto-generated catch block
89             e.printStackTrace();
90         }
91     }
92 }

通过编写定制的ThreadFactory可以定制游Executor创建的线程的属性(后台、优先级、名称)

 

转载于:https://www.cnblogs.com/wubingshenyin/p/4446062.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值