Java并发编程-ThreadFactory接口
2016-06-05 15:44
369 查看
工厂模式是最常用的模式之一,它可以把对象创建工作集中化,好处大家都知道:改变对象的创建方式将会变得很容易,而且可以控制创建对象的数量。
线程工厂(ThreadFactory接口)
在创建线程的时候,我们当然也能使用工厂模式来生产Thread,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并发编程实战,展示一个简单的Thread工厂模式来创建自己的Thread。工厂模式来创建自己的Thread。在创建线程的同时,记录线程创建的个数,并记录每个线程创建的时间等信息。
运行结果:
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
线程工厂(ThreadFactory接口)
在创建线程的时候,我们当然也能使用工厂模式来生产Thread,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并发编程实战,展示一个简单的Thread工厂模式来创建自己的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<String> stats; public MyThreadFactory(String name) { counter = 0; this.name = name; stats = new ArrayList<String>(); } @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<String> 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
相关文章推荐
- JAVAWeb开发学习笔记
- springMVC+Java验证码完善注册功能
- java多线程之对ThreadLocal类的理解
- Spring+Struts2+Mybatis整合
- java 输出九九乘法表
- java 抽象类 接口
- myeclipse10 提示unable to load default svn client
- Struts2+spring+Mybatis框架整合
- Ubuntu 15.04 安装JDK并配置成为默认的JDK
- [导入]Eclipse 导入/编译 Hadoop 源码
- JAVA 多线程静态同步函数的锁是class 对象
- java反射机制详解
- Java字符串处理之详解String自带方法
- 修饰类的使用以及使用技巧
- SpringMVC整合fastjson
- Java 8 日期时间 API
- Eclipse过时了吗_图文详解如何让Eclipse媲美IDEA
- Mac环境下JAVA开发环境搭建
- Java多线程编程(第四章)
- Java并发编程-线程局部变量(ThreadLocal)