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

来源:互联网 发布:淘宝客教学 编辑:程序博客网 时间:2024/05/29 05:12
http://zhangjunhd.blog.51cto.com/113473/700681. 类 Executors此类中提供的一些方法有:1.1 public static ExecutorService newCachedThreadPool()创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。对于执行很多短期异步任务的程序而言,这些线程池通常可提高程序性能。1.2 public static ExecutorService newFixedThreadPool(int nThreads)创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。1.3 public static ExecutorService newSingleThreadExecutor()创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。这三个方法都可以配合接口ThreadFactory的实例一起使用。并且返回一个ExecutorService接口的实例。2. 接口 ThreadFactory根据需要创建新线程的对象。使用线程工厂就无需再手工编写对 new Thread 的调用了,从而允许应用程序使用特殊的线程子类、属性等等。此接口最简单的实现就是:class SimpleThreadFactory implements ThreadFactory {   public Thread newThread(Runnable r) {     return new Thread(r);   } }3. 接口ExecutorService该接口提供了管理终止的方法。4.创建标准线程池启动线程4.1 提供一个简单的实现Runnable接口的线程MyThread.javapackage com.zj.concurrency.executors;public class MyThread implements Runnable {    private int count = 1, number;    public MyThread(int num) {       number = num;       System.out.println("Create Thread-" + number);    }    public void run() {       while (true) {           System.out.println("Thread-" + number + " run " + count+" time(s)");           if (++count == 3)              return;       }    }}这个线程会打印出相应的创建和执行信息。4.2使用CachedThreadPool启动线程CachedThreadPool.javapackage com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class CachedThreadPool {    public static void main(String[] args) {       ExecutorService exec = Executors.newCachedThreadPool();       for (int i = 0; i < 5; i++)           exec.execute(new MyThread(i));       exec.shutdown();    }}结果:Create Thread-0Create Thread-1Create Thread-2Create Thread-3Thread-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-4Thread-4 run 1 time(s)Thread-4 run 2 time(s)Thread-3 run 1 time(s)Thread-3 run 2 time(s)4.3 使用FixedThreadPool启动线程FixedThreadPool.javapackage com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class FixedThreadPool {    public static void main(String[] args) {       ExecutorService exec = Executors.newFixedThreadPool(2);       for (int i = 0; i < 5; i++)           exec.execute(new MyThread(i));       exec.shutdown();    }}结果:Create Thread-0Create Thread-1Create Thread-2Create Thread-3Create Thread-4Thread-0 run 1 time(s)Thread-0 run 2 time(s)Thread-2 run 1 time(s)Thread-2 run 2 time(s)Thread-3 run 1 time(s)Thread-3 run 2 time(s)Thread-4 run 1 time(s)Thread-4 run 2 time(s)Thread-1 run 1 time(s)Thread-1 run 2 time(s)4.4 使用SingleThreadExecutor启动线程SingleThreadExecutor.javapackage com.zj.concurrency.executors;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class SingleThreadExecutor {    public static void main(String[] args) {       ExecutorService exec = Executors.newSingleThreadExecutor();       for (int i = 0; i < 5; i++)           exec.execute(new MyThread(i));       exec.shutdown();    }}结果:Create Thread-0Create Thread-1Create Thread-2Create Thread-3Create Thread-4Thread-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)Thread-3 run 1 time(s)Thread-3 run 2 time(s)Thread-4 run 1 time(s)Thread-4 run 2 time(s)5.配合ThreadFactory接口的使用我们试图给线程加入daemon和priority的属性设置。5.1设置后台线程属性DaemonThreadFactory.javapackage 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.javapackage 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.javapackage 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.javapackage 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-0Create Thread-1Create Thread-2Create Thread-3Thread-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-4Thread-4 run 1 time(s)Thread-4 run 2 time(s)Create Thread-5Thread-5 run 1 time(s)Thread-5 run 2 time(s)Create Thread-6Create Thread-7Thread-7 run 1 time(s)Thread-7 run 2 time(s)Create Thread-8Thread-8 run 1 time(s)Thread-8 run 2 time(s)Create Thread-9Create Thread-10Thread-10 run 1 time(s)Thread-10 run 2 time(s)Create Thread-11Thread-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-12Create Thread-13Create Thread-14Thread-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-15Thread-15 run 1 time(s)Thread-15 run 2 time(s)Create Thread-16Thread-16 run 1 time(s)Thread-16 run 2 time(s)Create Thread-17Create Thread-18Create Thread-19Thread-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)本文出自 “i张俊” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/70068
原创粉丝点击