AnThreadGroupFactoryworkinmutiThread计算机等级考试

文章作者 100test 发表时间 2010:01:01 12:24:32
来源 100Test.Com百考试题网


  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.
  *
  */
  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=0.
  }
  //use parent thread group
  public ThreadGroupFactory(String namePrefix) {
  this(Thread.currentThread().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).
  thread.setDaemon(daemonFlag).
  return thread.
  }
  }

  编辑特别推荐:

  Java每日一题系列



相关文章


Java中取得当月最后一天的四种方法计算机等级考试
Java线程:并发协作(生产者消费者模型)计算机等级考试
Java线程:线程的同步(同步块)计算机等级考试
ServerTransportmodel计算机等级考试
AnThreadGroupFactoryworkinmutiThread计算机等级考试
JAVAAnnotation入门知识简介计算机等级考试
计算机二级JAVA辅导:JPA学习笔记(二)计算机等级考试
计算机二级JAVA辅导:JPA学习笔记(一)计算机等级考试
编程基础:Java实现硬盘文件搜索方法计算机等级考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛