• 多线程如何按指定顺序同步执行


    笔者今天看到一个有趣的面试题,如何让多个线程按照既定的顺序依次执行?比如每个线程输出一个整数,

    那么期望就是这样的:0,1,2,3,4,5,6,7,8,9. 而不是0,2,4,1,3,5,8,7,9,6

    乍一看,这不是反人性的考题吗?多线程本来就以乱序执行出名的。稍加思索,想到3种解决方案,分别用代码实现之。

     

    方法1 使用newSingleThreadExecutor

    newSingleThreadExecutor返回仅仅包含一个线程的线程池,将多个任务交给此Executor时,这个线程池处理完一个任务后接着处理下一个任务,这样就保证了执行顺序,先提交先执行。如果当前线程意外终止,会创建一个新线程继续执行任务。

    示例代码如下:

    ExecutorService pool = Executors.newSingleThreadExecutor();
            for(int i=0;i<1000;++i) {
                final int number = i;
                pool.execute(()-> {
                    System.out.println("I am " + number);
                } );
            }
    pool.shutdown();

    方法2 使用join方法

    When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution.

    英语原版其实很拗口,不好理解。简单点说,就是某个线程A调用join,其他线程就要乖乖等A执行完毕才能执行。

    示例代码如下:

    public class Worker implements Runnable {
    
        private int number;
        public Worker(int i) {
            number = i;
        }
        
        @Override
        public synchronized void run() {
            System.out.println("I am " + number);
        }
    }
    public class TestWorker {
    
        public static void main(String[] args) {
            
            for(int j=0;j<1000;++j) {
                Thread thread = new Thread(new Worker(j));
                thread.start();
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    方法3 使用ThreadPoolExecutor,设置它的核心线程数为1

    我们先分析一下ThreadPoolExecutor,其构造函数如下

    public ThreadPoolExecutor(int corePoolSize,

                                  int maximumPoolSize,

                                  long keepAliveTime,

                                  TimeUnit unit,

                                  BlockingQueue<Runnable> workQueue,

                                  ThreadFactory threadFactory,

                                  RejectedExecutionHandler handler)

    各个参数含义如下:

    1、corePoolSize, 核心线程数,建议和cpu的核心数一样,当有任务提交,检测当前线程池内的线程数小于corePoolSize的话,新建线程执行任务,直到达到corePoolSize。线程池内的线程数大于等于corePoolSize时,将任务放入workQueue等待。

    2、maximumPoolSize,允许线程池内最大线程数,当队列满了之后,如果线程池内的线程数小于maximumPoolSize新建线程,如果大于等于执行拒绝策略。

    3、keepAliveTime,线程最大空闲时间,如果设置60s,那么线程空闲60s后自动结束。

    unit,时间单位分钟,秒等等。

    4、workQueue,线程数超过corePoolSize存放任务的地方。

    5、threadFactory,线程工厂,默认的即可。

    6、handler,拒绝策略,分4种,AbortPolicy直接抛出异常、DiscardPolicy悄悄抛弃不执行、CallerRunsPolicy(调用者运行):该策略既不会抛弃任务也不会抛出异常,而是将这个任务退回给调用者,从而降低新任务的流量;、DiscardOldestPolicy(抛弃最旧的)

     示例代码如下:

    ExecutorService pool = new ThreadPoolExecutor(1, 1000, 300, TimeUnit.SECONDS, 
                    new LinkedBlockingQueue<Runnable>(1000),Executors.defaultThreadFactory(), 
                    new ThreadPoolExecutor.AbortPolicy());
            for(int i=0;i<1000;++i) {
                final int number = i;
                pool.execute(()-> {
                    System.out.println("I am " + number);
                } );
            }
    pool.shutdown();

    4. 执行结果

    I am 0
    I am 1
    I am 2
    I am 3
    I am 4
    I am 5
    I am 6
    I am 7
    I am 8
    I am 9
    I am 10
    
    。。。
    
    I am 990
    I am 991
    I am 992
    I am 993
    I am 994
    I am 995
    I am 996
    I am 997
    I am 998
    I am 999
  • 相关阅读:
    Eureka 原理圖
    RabbitMQ 介紹
    rabbitmq-3.5.1-安裝
    MyBatis 基础入门
    CSS
    程序员必会算法-KMP算法
    编程这些年
    排序算法之直接插入排序
    排序算法之选择排序
    排序算法之冒泡排序
  • 原文地址:https://www.cnblogs.com/sankt/p/11695676.html
Copyright © 2020-2023  润新知