package thread;
import ncurrent
/**
* Thread Factory with Thread Group
* can create thread by daemon flag
*
* @author daniel zhou
*
*/
class ThreadGroupFactory implements ThreadFactory{
//attributes
private ThreadGroup _threadGroup;
private String _namePrefix;
private boolean _createDaemonFlag;
private int _numThreads;
private final Object _synLock = new Object();
//assign the thread group
public ThreadGroupFactory(ThreadGroup threadGroup
_threadGroup=threadGroup;
_namePrefix = namePrefix;
_numThreads=
}
//use parent thread group
public ThreadGroupFactory(String namePrefix) {
this(Thread
}
//assign the daemon flag
public void createDaemonThreads(boolean createDaemonFlag){
synchronized(_synLock){
_createDaemonFlag=createDaemonFlag;
}
}
@Override
public Thread newThread(Runnable r) {
String threadName;
boolean daemonFlag;
synchronized(_synLock){
threadName = _namePrefix + ++_numThreads;
daemonFlag = _createDaemonFlag;
}
Thread thread = new Thread(_threadGroup
thread
return thread;
}
}
本文出自
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26403.html