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

  • 相关阅读:
    8月3日云栖精选夜读:LSF-SCNN:一种基于CNN的短文本表达模型及相似度计算的全新优化模型
    linux(centos)下安装PHP的PDO扩展
    Linux中find常见用法示例
    01 编译原理概述
    20145221 《信息安全系统设计基础》第5周学习总结
    20145221 《信息安全系统设计基础》第4周学习总结
    爱春秋之戏说春秋 Writeup
    20145221 《信息安全系统设计基础》第3周学习总结
    20145221 《信息安全系统设计基础》第2周学习总结
    20145221 《信息安全系统设计基础》第1周学习总结
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/11655314.html
Copyright © 2020-2023  润新知