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

  • 相关阅读:
    【原创】Silverlight之TextBox的LostFocus、GotFocus事件
    SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]
    cannot convert from 'wchar_t *' to 'char *' 问题
    TEXTMETRICW 结构记录
    【D3D】Direct3D中LPRECT(上左右底)和LPoint(x,y)之前转换
    【原创】有关Silverlight中自动生成的类中 没有WCF层edmx模型新加入的对象 原因分析。
    【原创】有关Silverlight中异常“XmalParseEception” 通用解决思路
    hdu 1011(Starship Troopers,树形dp)
    hdu 2196(Computer 树形dp)
    树形dp(poj 1947 Rebuilding Roads )
  • 原文地址:https://www.cnblogs.com/zhuyeshen/p/11655314.html
Copyright © 2011-2022 走看看