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()来创建线程池同时放入多个线程时,每个线程都会按照自己的调度来执行,但是当其中一个线程被阻塞时,其它的线程都会受到影响被阻塞,不过依然都会按照自身调度来执行,但是会存在阻塞延迟。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值