• 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()方法来创建线程。
  • 相关阅读:
    jQuery Dialog and timepicker显示层的问题
    js、PHP将分数字符串转换为小数
    jqgrid动态显示/隐藏某一列
    Oracle查询每天固定时间段的数据
    Python安装pandas
    Python version 2.7 required, which was not found in the registry
    python刷剑指offer(1-20)(一刷)
    图像预处理(含计算机视觉概述)
    案例分析
    (七)目标检测算法之SSD
  • 原文地址:https://www.cnblogs.com/yaowen/p/6069628.html
Copyright © 2020-2023  润新知