一、使用ThreadFactory的好处
- 1、更容易修改类,或者改变对象的方式
- 2、更容易为有限资源限制创建对象的数目
- 3、更容易为创建的对象生成统计工具
二、ThreadFactory接口
//这个接口只有一个方法
public interface ThreadFactory {
/**
* Constructs a new {@code Thread}. Implementations may also initialize
* priority, name, daemon status, {@code ThreadGroup}, etc.
*
* @param r a runnable to be executed by new thread instance
* @return constructed thread, or {@code null} if the request to
* create a thread is rejected
*/
Thread newThread(Runnable r);
}
三、使用实例
package com.concurrent.threadManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadFactory;
public class MyThreadFactory implements ThreadFactory {
private int counter;