Java 多线程开发之Executors

java自带进程监控工具

C:Program FilesJavajdk1.8.0_141injconsole.exe

例子1 固定数量线程池

package testExecutors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTestExe {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        test1();
    }
    
    private static void test1() {
        // TODO Auto-generated method stub
        ExecutorService pool=Executors.newFixedThreadPool(3);
        Thread t1=new MyThread(1);
        Thread t2=new MyThread(2);
        Thread t3=new MyThread(3);
        Thread t4=new MyThread(4);
        Thread t5=new MyThread(5);
        pool.execute(t1);
        pool.execute(t2);
        pool.execute(t3);
        pool.execute(t4);
        pool.execute(t5);
        pool.shutdown();
    }
    private static void test2() {
        // TODO Auto-generated method stub
        
    }
}
class MyThread extends Thread{
    public int no;
    public MyThread(int i) {
        // TODO Auto-generated constructor stub
        this.no=i;
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("task"+this.no+" begins...");
        try {
            sleep(8000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("task"+this.no+" ends...");
    }
}

说明:任务类MyThread在任务起始打印数据,耗时8秒钟。

pool只有三个线程可用,在前三个任务执行完毕之后才运行剩下的2个

运行结果

task2 begins...
task1 begins...
task3 begins...
task2 ends...
task1 ends...
task4 begins...
task5 begins...
task3 ends...
task5 ends...
task4 ends...
例子1运行结果

例子2:单线程线程池

将test1函数第一行代码改成

 ExecutorService pool = Executors.newSingleThreadExecutor();

运行结果

一个任务运行结束才开始下一个

本例子和例子1的相同之处:当加入的线程超过最大尺寸,其他线程需要等待。

task1 begins...
task1 ends...
task2 begins...
task2 ends...
task3 begins...
task3 ends...
task4 begins...
task4 ends...
task5 begins...
task5 ends...
例子2运行结果

例子3:可变线程池

ExecutorService pool = Executors.newCachedThreadPool();

根据需要创建线程(如果先前创建的线程可用,会复用之)

60秒没有使用的线程会被终止和移除

特别适合短寿命的异步任务,

运行结果:创建了5个线程,几乎同时运行。

task1 begins...
task4 begins...
task3 begins...
task2 begins...
task5 begins...
task4 ends...
task1 ends...
task5 ends...
task3 ends...
task2 ends...
View Code

例子4 延迟执行线程池

test1中代码改成如下:

private static void test1() {
        // TODO Auto-generated method stub
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        Thread t1=new MyThread(1);
        Thread t2=new MyThread(2);
        Thread t3=new MyThread(3);
        Thread t4=new MyThread(4);
        Thread t5=new MyThread(5);
        pool.execute(t1);
        pool.execute(t2);
        pool.execute(t3);
        //pool.execute(t4);
        //pool.execute(t5);
        pool.schedule(t4, 10, TimeUnit.SECONDS);
        pool.schedule(t5,10,TimeUnit.SECONDS);
        pool.shutdown();
    }

说明:本例子和例子1很相似,线程池中只有2个可用线程。

任务4和任务5在等待10秒钟之后被使能,

原文地址:https://www.cnblogs.com/legion/p/8820442.html