首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:Java:使用Executors创建和管理线程

Java:使用Executors创建和管理线程

www.jz123.cn  2009-09-08   来源:   IT专家网    责任编辑(袁袁)    我要投递新闻

 5.配合ThreadFactory接口的使用

  我们试图给线程加入daemon和priority的属性设置。

  5.1设置后台线程属性


 DaemonThreadFactory.java
  package com.zj.concurrency.executors.factory;
  import java.util.concurrent.ThreadFactory;
  public class DaemonThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
  Thread t = new Thread(r);
  t.setDaemon(true);
  return t;
  }
  }

  5.2 设置优先级属性


     最高优先级MaxPriorityThreadFactory.java
  package com.zj.concurrency.executors.factory;
  import java.util.concurrent.ThreadFactory;
  public class MaxPriorityThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
  Thread t = new Thread(r);
  t.setPriority(Thread.MAX_PRIORITY);
  return t;
  }
  }


   最低优先级MinPriorityThreadFactory.java
  package com.zj.concurrency.executors.factory;
  import java.util.concurrent.ThreadFactory;
  public class MinPriorityThreadFactory implements ThreadFactory {
  public Thread newThread(Runnable r) {
  Thread t = new Thread(r);
  t.setPriority(Thread.MIN_PRIORITY);
  return t;
  }
  }

  5.3启动带有属性设置的线程

 


 ExecFromFactory.java
  package com.zj.concurrency.executors;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import com.zj.concurrency.executors.factory.DaemonThreadFactory;
  import com.zj.concurrency.executors.factory.MaxPriorityThreadFactory;
  import com.zj.concurrency.executors.factory.MinPriorityThreadFactory;
  public class ExecFromFactory {
  public static void main(String[] args) throws Exception {
  ExecutorService defaultExec = Executors.newCachedThreadPool();
  ExecutorService daemonExec = Executors
  .newCachedThreadPool(new DaemonThreadFactory());
  ExecutorService maxPriorityExec = Executors
  .newCachedThreadPool(new MaxPriorityThreadFactory());
  ExecutorService minPriorityExec = Executors
  .newCachedThreadPool(new MinPriorityThreadFactory());
  for (int i = 0; i < 10; i++)
  daemonExec.execute(new MyThread(i));
  for (int i = 10; i < 20; i++)
  if (i == 10)
  maxPriorityExec.execute(new MyThread(i));
  else if (i == 11)
  minPriorityExec.execute(new MyThread(i));
  else
  defaultExec.execute(new MyThread(i));
  }
  }

  结果:

  Create Thread-0

  Create Thread-1

  Create Thread-2

  Create Thread-3

  Thread-0 run 1 time(s)

  Thread-0 run 2 time(s)

  Thread-1 run 1 time(s)

  Thread-1 run 2 time(s)

  Thread-2 run 1 time(s)

  Thread-2 run 2 time(s)

  Create Thread-4

  Thread-4 run 1 time(s)

  Thread-4 run 2 time(s)

  Create Thread-5

  Thread-5 run 1 time(s)

  Thread-5 run 2 time(s)

  Create Thread-6

  Create Thread-7

  Thread-7 run 1 time(s)

  Thread-7 run 2 time(s)

  Create Thread-8

  Thread-8 run 1 time(s)

  Thread-8 run 2 time(s)

  Create Thread-9

  Create Thread-10

  Thread-10 run 1 time(s)

  Thread-10 run 2 time(s)

  Create Thread-11

  Thread-9 run 1 time(s)

  Thread-9 run 2 time(s)

  Thread-6 run 1 time(s)

  Thread-6 run 2 time(s)

  Thread-3 run 1 time(s)

  Thread-3 run 2 time(s)

  Create Thread-12

  Create Thread-13

  Create Thread-14

  Thread-12 run 1 time(s)

  Thread-12 run 2 time(s)

  Thread-13 run 1 time(s)

  Thread-13 run 2 time(s)

  Create Thread-15

  Thread-15 run 1 time(s)

  Thread-15 run 2 time(s)

  Create Thread-16

  Thread-16 run 1 time(s)

  Thread-16 run 2 time(s)

  Create Thread-17

  Create Thread-18

  Create Thread-19

  Thread-14 run 1 time(s)

  Thread-14 run 2 time(s)

  Thread-17 run 1 time(s)

  Thread-17 run 2 time(s)

  Thread-18 run 1 time(s)

  Thread-18 run 2 time(s)

  Thread-19 run 1 time(s)

  Thread-19 run 2 time(s)

  Thread-11 run 1 time(s)

  Thread-11 run 2 time(s)

  本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/70068

上一篇:Java:使用synchronized和Lock对象获取对象锁 下一篇:Java面试技巧:Java面试题集锦(三)

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图