package thread;

import java.util.concurrent.ThreadFactory;

* Thread Factory with Thread Group, which can work in multi thread env,    
* can create thread by daemon flag, and named each thread in creating.
* @author daniel zhou

class ThreadGroupFactory implements ThreadFactory{
  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, String namePrefix) {
    _namePrefix = namePrefix;

  //use parent thread group
  public ThreadGroupFactory(String namePrefix) {
                this(Thread.currentThread().getThreadGroup(), namePrefix);
  //assign the daemon flag
  public void createDaemonThreads(boolean createDaemonFlag){

  public Thread newThread(Runnable r) {
    String threadName;
    boolean daemonFlag;
      threadName = _namePrefix + ++_numThreads;
      daemonFlag = _createDaemonFlag;
    Thread thread = new Thread(_threadGroup, r, threadName);
    return thread;