zl程序教程

您现在的位置是:首页 >  其它

当前栏目

ExecutorService

ExecutorService
2023-09-14 09:00:00 时间
ExecuteService是Executors创建的线程池
表述了异步执行的机制,并且可以让任务在后台执行
线程池体系结构
java.util.concurrent.Executor 负责线程的使用和调度的根接口
        |--ExecutorService 子接口: 线程池的主要接口
                |--ThreadPoolExecutor 线程池的实现类
                |--ScheduledExceutorService 子接口: 负责线程的调度
                    |--ScheduledThreadPoolExecutor : 继承ThreadPoolExecutor,实现了ScheduledExecutorService
4种类型线程池
ExecutorService newFixedThreadPool() : 创建固定大小的线程池
ExecutorService newCachedThreadPool() : 缓存线程池,线程池的数量不固定,可以根据需求自动的更改数量。
ExecutorService newSingleThreadExecutor() : 创建单个线程池。 线程池中只有一个线程
ScheduledExecutorService newScheduledThreadPool() : 创建固定大小的线程,可以延迟或定时的执行任务

常用的方法

(1)

execute(Runnable) 以异步的方式执行

eg:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Demo{
    public static void main(String[] args) {
        System.out.println("start");
        ExecutorService es1 = Executors.newSingleThreadExecutor();
        es1.execute(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("子线程:"+Thread.currentThread().getName()+"执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        es1.shutdown();
        System.out.println("end");
    }
}

输出  

start
end
子线程:pool-1-thread-1执行

(2)

submit(Runnable) 会返回Future 对象。Future 对象可以用于判断 Runnable 是否结束执行

eg:

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

public class Demo {

    public static void main(String[] args) {
        System.out.println("start");
        ExecutorService es1 = Executors.newSingleThreadExecutor();
        Future future = es1.submit(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("子线程:" + Thread.currentThread().getName() + "执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        try {
            //如果任务结束执行则返回 null
            System.out.println("future.get()=" + future.get());
        }catch (Exception e) {
            e.printStackTrace();
        }
        es1.shutdown();
        System.out.println("end");

    }

}

输出

start
子线程:pool-1-thread-1执行
future.get()=null
end

(3)

submit(Callable) 会返回Future 对象。Future 对象可以用于判断 Callable是否结束执行

eg:

import java.util.concurrent.*;

public class Demo {

    public static void main(String[] args) {
        System.out.println("start");
        ExecutorService es1 = Executors.newSingleThreadExecutor();
        Future future = es1.submit(new Callable() {
            public Object call(){
                try {
                    Thread.sleep(1000);
                    System.out.println("子线程:" + Thread.currentThread().getName() + "执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "Callable Result";
            }
        });
        try {
            System.out.println("future.get()=" + future.get());
        }catch (Exception e) {
            e.printStackTrace();
        }
        es1.shutdown();
        System.out.println("end");
    }

}

(4)

shutdown() 关闭

shutdownNow() 立即关闭

存在于 ExecutorService 中的活动线程会阻止Java虚拟机关闭 

为了关闭在 ExecutorService 中的线程,需要调用 shutdown() 方法。

ExecutorService 并不会马上关闭,而是不再接收新的任务,但所有的线程结束执行当前任务,ExecutorServie 才会真的关闭

所有在调用 shutdown() 方法之前提交到 ExecutorService 的任务都会执行