熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

An Thread Group Factory work&

2013-11-23 19:11:04  來源: Java核心技術 



package thread;

import ncurrentThreadFactory;

/**
* 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{
  //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 String namePrefix) {
    
    _threadGroup=threadGroup;
    _namePrefix = namePrefix;
    _numThreads=;
  }

  //use parent thread group
  public ThreadGroupFactory(String namePrefix) {
    
                this(ThreadcurrentThread()getThreadGroup() namePrefix);
                
        }
    
  //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 r threadName);
    threadsetDaemon(daemonFlag);
    
    return thread;
  }
    
}

  本文出自 專注JEE系列規范下的開源技術 博客請務必保留此出處


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26403.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.