zoukankan      html  css  js  c++  java
  • 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()方法来创建线程。
  • 相关阅读:
    数组初始化
    排序算法
    fast rcnn,faster rcnn使用cudann加速问题
    「不啰嗦」和「说清楚」-20141223早读课
    加州理工学院公开课:机器学习与数据挖掘_Regularization(第十二课)
    2014年百度之星程序设计大赛
    一个伟大的发现,装X一下。笔记本win7系统64位机器执行unity 时,屏幕模糊解决的方法
    面向对象基础——类与对象的定义
    hdu1325 Is It A Tree?(二叉树的推断)
    持续集成(CI)工具------Hudson/Jenkins(Continuous Integration)安装与配置具体解释
  • 原文地址:https://www.cnblogs.com/yaowen/p/6069628.html
Copyright © 2011-2022 走看看