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()方法来创建线程。
  • 相关阅读:
    java基础
    JAVASE 安装步骤
    MySQL 45道练习题
    MySQL 多表查询
    2018-08-07JDBC连接MySQL+增删改表格+SQL注入问题及其预处理对象PreparedStatement解决方案
    2018-08-06Java中的异常捕获和Throw详细细节
    2018-08-03List接口方法+LinkedList方法+Vector集合+Set接口下HashSet和LinkedHashSet集合+HashCode()+equals()方法对于Set接口判断重复的详细细节
    2018-08-01集合Collection+Iterator迭代器+泛型+增强For循环
    2018-07-31包装类与基本数据类型String的转换+System类详细知识+Arrays类+大数据(BigInteger+BigDecimal)运算
    2018-07-27Final+Static+匿名对象+3中代码块
  • 原文地址:https://www.cnblogs.com/yaowen/p/6069628.html
Copyright © 2011-2022 走看看