Executors.newSingleThreadScheduledExecutor();线程池中放入多个线程问题

时间:2022-07-23
本文章向大家介绍Executors.newSingleThreadScheduledExecutor();线程池中放入多个线程问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by LiChao on 2017/9/29
 */

public class Test3 {

    private static long start = new Date().getTime();

    private static final ScheduledExecutorService excutor = Executors.newSingleThreadScheduledExecutor();



    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            long end = new Date().getTime();
            System.out.println("time wait:"+(end-start)+",this is 线程1");
        },"线程1");

        Thread thread2 = new Thread(() -> {
            long end = new Date().getTime();
            System.out.println("time wait:"+(end-start)+",this is 线程2");
        },"线程2");

        Thread thread3 = new Thread(() -> {
            long end = new Date().getTime();
            System.out.println("time wait:"+(end-start)+",this is 线程3");
        },"线程3");
        excutor.scheduleWithFixedDelay(thread1,0, 1 , TimeUnit.SECONDS);
        excutor.scheduleWithFixedDelay(thread2,0, 2 , TimeUnit.SECONDS);
        excutor.scheduleWithFixedDelay(thread3,0, 3 , TimeUnit.SECONDS);
    }

}

输出结果:

time wait:80,this is 线程1
time wait:80,this is 线程2
time wait:80,this is 线程3
time wait:1088,this is 线程1
time wait:2081,this is 线程2
time wait:2089,this is 线程1
time wait:3081,this is 线程3
time wait:3090,this is 线程1
time wait:4082,this is 线程2
time wait:4091,this is 线程1
time wait:5092,this is 线程1
time wait:6082,this is 线程3
time wait:6083,this is 线程2
time wait:6093,this is 线程1
time wait:7094,this is 线程1
time wait:8084,this is 线程2
time wait:8095,this is 线程1
time wait:9083,this is 线程3
time wait:9095,this is 线程1

线程池executor调用scheduleWithFixedDelay方法,同时放入三个不同调度的线程。从结果中可以看出每个线程按照自己的调度互不干扰的运行。此时修改线程2加一个阻塞再看看运行结果。

Thread thread2 = new Thread(() -> {
            long end = new Date().getTime();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("time wait:"+(end-start)+",this is 线程2");
        },"线程2");

运行结果:

time wait:129,this is 线程1
time wait:130,this is 线程2
time wait:3130,this is 线程3
time wait:3130,this is 线程1
time wait:4131,this is 线程1
time wait:5131,this is 线程2
time wait:8131,this is 线程1
time wait:8131,this is 线程3
time wait:9132,this is 线程1
time wait:10132,this is 线程2
time wait:13132,this is 线程1
time wait:13132,this is 线程3
time wait:14133,this is 线程1
time wait:15133,this is 线程2
time wait:18133,this is 线程1
time wait:18133,this is 线程3
time wait:19134,this is 线程1
time wait:20134,this is 线程2
time wait:23142,this is 线程1
time wait:23142,this is 线程3

从结果中可以看出,当线程2被阻塞时,其它的线程也被阻塞不能运行。所以使用Executors.newSingleThreadScheduledExecutor()来创建线程池同时放入多个线程时,每个线程都会按照自己的调度来执行,但是当其中一个线程被阻塞时,其它的线程都会受到影响被阻塞,不过依然都会按照自身调度来执行,但是会存在阻塞延迟。