Java并发编程-ThreadFactory接口

工厂模式是最常用的模式之一,它可以把对象创建工作集中化,好处大家都知道:改变对象的创建方式将会变得很容易,而且可以控制创建对象的数量。

  • 线程工厂(ThreadFactory接口)

    在创建线程的时候,我们当然也能使用工厂模式来生产线程,ThreadFactory是用来实现创建线程的工厂接口,其实它只有一个方法Thread newThread(Runnable r),所以这个接口没多大用,可以自己编写新接口。

    使用ThreadFactory工厂能替代默认的new Thread,而且在自定义工厂里面,我们能创建自定义化的Thread,并且计数,或则限制创建Thread的数量,给每个Thread设置对应的好听的名字,或则其他的很多很多事情。

  • ThreadFactory在并发中的使用

    • 在Java中使用ThreadFactory最多应该就是Executor框架和Fork/Join框架了,使用ThreadFactory创建各种线程池中的线程。在Executor框架中,使用Executors创建线程池执行器的时候,也可以传入ThreadFactory对象,执行器将会使用该ThreadFactory对象来创建线程。
      如:newCachedThreadPool(ThreadFactory threadFactory)
  • 使用实例
    下面的例子来自《Java并发编程实战》,展示一个简单的使用ThreadFactory来创建自己的Thread。工厂模式来创建自己的Thread。在创建线程的同时,记录线程创建的个数,并记录每个线程创建的时间等信息。

package MyThread;

import java.util.concurrent.ThreadFactory;
import java.util.ArrayList;  
import java.util.Date;  
import java.util.Iterator;  
import java.util.List;  


public class MyThreadFactory implements ThreadFactory {  

    private int counter;  
    private String name;  
    private List stats;  

    public MyThreadFactory(String name) {  
        counter = 0;  
        this.name = name;  
        stats = new ArrayList();  
    }  

    @Override  
    public Thread newThread(Runnable run) {  
        Thread t = new Thread(run, name + "-Thread-" + counter);  
        counter++;  
        stats.add(String.format("Created thread %d with name %s on%s\n" ,t.getId() ,t.getName() ,new Date()));  
        return t;  
    }  

    public String getStas() {  
        StringBuffer buffer = new StringBuffer();  
        Iterator it = stats.iterator();  
        while(it.hasNext()) {  
            buffer.append(it.next());  
         //   buffer.append("\n");  
        }  
        return buffer.toString();  
    }  

    public static void main(String[] args) {  
        MyThreadFactory factory = new MyThreadFactory("MyThreadFactory");  
        Task task = new Task();  
        Thread thread = null;  
        for(int i = 0; i < 10; i++) {  
            thread = factory.newThread(task);  
            thread.start();  
        }  
        System.out.printf("Factory stats:\n");  
        System.out.printf("%s\n",factory.getStas());  


    }  

}  
class Task implements Runnable{

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }

}

运行结果:
Factory stats:
Created thread 9 with name MyThreadFactory-Thread-0 onSun Jun 05 15:42:25 CST 2016
Created thread 10 with name MyThreadFactory-Thread-1 onSun Jun 05 15:42:25 CST 2016
Created thread 11 with name MyThreadFactory-Thread-2 onSun Jun 05 15:42:25 CST 2016
Created thread 12 with name MyThreadFactory-Thread-3 onSun Jun 05 15:42:25 CST 2016
Created thread 13 with name MyThreadFactory-Thread-4 onSun Jun 05 15:42:25 CST 2016
Created thread 14 with name MyThreadFactory-Thread-5 onSun Jun 05 15:42:25 CST 2016
Created thread 15 with name MyThreadFactory-Thread-6 onSun Jun 05 15:42:25 CST 2016
Created thread 16 with name MyThreadFactory-Thread-7 onSun Jun 05 15:42:25 CST 2016
Created thread 17 with name MyThreadFactory-Thread-8 onSun Jun 05 15:42:25 CST 2016
Created thread 18 with name MyThreadFactory-Thread-9 onSun Jun 05 15:42:25 CST 2016

你可能感兴趣的:(java,并发,多线程,Thread,工厂模式,Java并发编程,Java并发编程)