ThreadFactory

根据需要创建新线程的对象。使用线程工厂就无需再手工编写对 new Thread 的调用了,从而允许应用程序使用特殊的线程子类、属性等等。
 
JDK中的介绍:

An object that creates new threads on demand. Using thread factories removes hardwiring of calls tonew Thread, enabling applications to use special thread subclasses, priorities, etc.

The simplest implementation of this interface is just:

 

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. class SimpleThreadFactory implements ThreadFactory {  
  2.   public Thread newThread(Runnable r) {  
  3.     return new Thread(r);  
  4.   }  
  5. }  

 

The Executors.defaultThreadFactory method provides a more useful simple implementation, that sets the created thread context to known values before returning it. 
 
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * The default thread factory 
  3.      */  
  4.     static class DefaultThreadFactory implements ThreadFactory {  
  5.         static final AtomicInteger poolNumber = new AtomicInteger(1);  
  6.         final ThreadGroup group;  
  7.         final AtomicInteger threadNumber = new AtomicInteger(1);  
  8.         final String namePrefix;  
  9.   
  10.         DefaultThreadFactory() {  
  11.             SecurityManager s = System.getSecurityManager();  
  12.             group = (s != null)? s.getThreadGroup() :  
  13.                                  Thread.currentThread().getThreadGroup();  
  14.             namePrefix = "pool-" +  
  15.                           poolNumber.getAndIncrement() +  
  16.                          "-thread-";  
  17.         }  
  18.   
  19.         public Thread newThread(Runnable r) {  
  20.             Thread t = new Thread(group, r,  
  21.                                   namePrefix + threadNumber.getAndIncrement(),  
  22.                                   0);  
  23.             if (t.isDaemon())  
  24.                 t.setDaemon(false);  
  25.             if (t.getPriority() != Thread.NORM_PRIORITY)  
  26.                 t.setPriority(Thread.NORM_PRIORITY);  
  27.             return t;  
  28.         }  
  29.     }  

下面写一简单示例。
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. package com.test;  
  2.   
  3. import java.util.concurrent.ExecutorService;  
  4. import java.util.concurrent.Executors;  
  5. import java.util.concurrent.ThreadFactory;  
  6.   
  7. class Task implements Runnable{  
  8.     int taskId;  
  9.     public Task(int taskId) {  
  10.         this.taskId=taskId;  
  11.     }  
  12.       
  13.     @Override  
  14.     public void run() {  
  15.         System.out.println(Thread.currentThread().getName()+"--taskId: "+taskId);  
  16.           
  17.     }  
  18. }  
  19.   
  20. class DaemonThreadFactory implements ThreadFactory {  
  21.     @Override  
  22.     public Thread newThread(Runnable r) {  
  23.         Thread t=new Thread(r);  
  24.         t.setDaemon(true);  
  25.         return t;  
  26.     }  
  27.       
  28. }  
  29. public class ThreadFactoryTest {  
  30.     public static void main(String[] args) {  
  31.         ExecutorService exec=Executors.newFixedThreadPool(3,new DaemonThreadFactory());  
  32.         for(int i=0;i<3;i++) {  
  33.             exec.submit(new Task(i));  
  34.         }  
  35.         exec.shutdown();  
  36.     }  
  37. }  

输出如下:
 
Thread-0--taskId: 0
Thread-1--taskId: 1
Thread-2--taskId: 2
 
分析:
DaemonThreadFactory中覆写的newThread()方法与submit()方法的调用关系,也就是说DaemonThreadFactory是如何起作用的。
调试输出其调用关系:
 
也就是说,submit()时会调用DaemonThreadFactory类的newThread()方法来创建线程。
posted @   无天666  阅读(1545)  评论(0编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
历史上的今天:
2015-11-16 android100 自定义内容提供者
点击右上角即可分享
微信分享提示