美文网首页
多线的顺序控制

多线的顺序控制

作者: NazgulSun | 来源:发表于2021-05-26 21:32 被阅读0次

基于JOIN

线程自己的逻辑与控制逻辑分离,执行逻辑不受影响,耦合性最低

        Thread t1 = new Thread(new T1());
        t1.start();
        t1.join();
        Thread t2 = new Thread(new T2());
        t2.start();
        t2.join();
        Thread t3 = new Thread(new T3());
        t3.start();
        t3.join();

基于信号量

每个线程需要知道自己运行所需要的信号量,以及开启下一个线程的信号量,
有点像 chain 模式。 执行逻辑和控制逻辑有耦合。


    public static void main(String[] args) throws Exception {

        Semaphore lock1 = new Semaphore(1);
        Semaphore lock2 = new Semaphore(1);
        Semaphore lock3 = new Semaphore(1);
        lock2.acquire();
        lock3.acquire();
        for(int i =0; i < 10; i++) {



            Thread t1 = new Thread(new T1(lock1,lock2));
            Thread t2 = new Thread(new T2(lock2, lock3));
            t2.start();
            Thread t3 = new Thread(new T3(lock3, lock1));
            t3.start();
            t1.start();
        }

    }


    static class T1 implements Runnable{

        public Semaphore nextLock;
        Semaphore current;
        public T1(Semaphore current, Semaphore nextlock){
            this.nextLock=nextlock;
            this.current =current;
        }

        @Override
        public void run() {
            try {
                current.acquire();
                System.out.println("Sleep");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-111");
            nextLock.release();
        }
    }

    static class T2 implements Runnable{
        public Semaphore nextLock;
        Semaphore current;
        public T2(Semaphore current, Semaphore nextlock){
            this.nextLock=nextlock;
            this.current =current;
        }

        @Override
        public void run() {
                try {
                    current.acquire();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-222");
                nextLock.release();
            }
    }

    static class T3 implements Runnable{

        public Semaphore nextLock;
        Semaphore current;
        public T3(Semaphore current, Semaphore nextlock){
            this.nextLock=nextlock;
            this.current =current;
        }

        @Override
        public void run() {
                try {
                    current.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-333");
                nextLock.release();
            }

    }

基于countDownlatch

和信号量的思路是一样的,每个线程需要等待自己 latch,并且完成任务之后,对下一个latch 进行 countDown。

    public static void main(String[] args) throws Exception {


        for(int i =0; i < 1; i++) {


            CountDownLatch c1 = new CountDownLatch(1);
            CountDownLatch c2 = new CountDownLatch(1);

            Thread t1 = new Thread(new T1(c1));
            Thread t2 = new Thread(new T2(c1,c2));
            t2.start();
            Thread t3 = new Thread(new T3(c2));
            t3.start();
            t1.start();
        }

    }


    static class T1 implements Runnable{

        public CountDownLatch c1;
        public T1(CountDownLatch c1){
            this.c1=c1;
        }

        @Override
        public void run() {
            try {
                System.out.println("Sleep");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-111");
            c1.countDown();
        }
    }

    static class T2 implements Runnable{
        public CountDownLatch c1;
        CountDownLatch c2;
        public T2(CountDownLatch c1, CountDownLatch c2){
            this.c1=c1;
            this.c2 =c2;
        }

        @Override
        public void run() {
                try {
                    c1.await();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-222");
                c2.countDown();
            }
    }

    static class T3 implements Runnable{

        CountDownLatch c2;
        public T3(CountDownLatch c2){
            this.c2 =c2;
        }

        @Override
        public void run() {
                try {
                    c2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-333");
            }

    }

基于条件变量

条件变量是lock+ condition, 是最接近用 notify+wait 来实现的方式。
每个lock 可以穿件多个条件变量, 每个 条件变量 await 都能够释放lock。
但是condition 的singal 方法可以精确的唤醒等待的线程。这是条件变量现对于notify的优势,可以更加精准的控制。


    public static void main(String[] args) throws Exception {


        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();

        Thread t1 = new Thread(new T1(lock, condition1));
        Thread t2 = new Thread(new T2(lock, condition1,condition2));
        t2.start();
        Thread t3 = new Thread(new T3(lock, condition2,null));
        t3.start();
        t1.start();

    }


    static class T1 implements Runnable{

        public Lock lock;
        Condition nextCondition;
        public T1(Lock lock, Condition nextCondition){
            this.lock = lock;
            this.nextCondition =nextCondition;
        }

        @Override
        public void run() {
            try {
                lock.lock();
                System.out.println("Sleep");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-111");
            nextCondition.signal();
            lock.unlock();
        }
    }

    static class T2 implements Runnable{

        public Lock lock;
        Condition nextCondition;
        Condition currentCondition;
        public T2(Lock lock, Condition currentCondition,Condition nextCondition){
            this.lock = lock;
            this.nextCondition =nextCondition;
            this.currentCondition = currentCondition;
        }

        @Override
        public void run() {

            lock.lock();
            try {
                currentCondition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-222");
            nextCondition.signal();
            lock.unlock();
        }
    }

    static class T3 implements Runnable{

        public Lock lock;
        Condition nextCondition;
        Condition currentCondition;
        public T3(Lock lock, Condition currentCondition,Condition nextCondition){
            this.lock = lock;
            this.nextCondition =nextCondition;
            this.currentCondition = currentCondition;
        }

        @Override
        public void run() {

            lock.lock();
            try {
                currentCondition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-333");
            lock.unlock();
        }
    }

相关文章

  • 多线的顺序控制

    基于JOIN 线程自己的逻辑与控制逻辑分离,执行逻辑不受影响,耦合性最低 基于信号量 每个线程需要知道自己运行所需...

  • 常用的GCD记录一下

    子线程并行 串行 主线程 串行队列 子线程 并行队列 子线程 栅栏函数 控制执行顺序 避免数据竞争 多线...

  • Android Dialog 控制弹窗展示顺序/有序弹出 (多线

    安利一个多线程连续触发器的框架,项目地址ContinuousTrigger 用于按序执行一系列任务,可随时绑定(如...

  • 线程

    Java 多线 Java 给多线程编程提供了内置的支持。 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以...

  • 临摹小多肉

    线稿,发现多肉画太大了。修改。 上色。 完成。 顺序都乱了。T﹏T

  • 多线盘两机两泵

    消防主机多线控制盘可以直接启泵吗? 可以的,前提是你泵柜的控制开关应设置在自动启泵的状态,如为手动状态,则即使多线...

  • 关于U3D DrawCall优化手记

    【一】渲染顺序 U3D的渲染是有顺序的,U3D的渲染顺序是由我们控制的,控制好U3D的渲染顺序,你才能控制好Dra...

  • Cocos Creator 脚本执行顺序(摘自官方文档)

    脚本执行顺序 完善的脚本执行顺序控制将在新版本中添加,目前请使用下面的原则控制脚本执行顺序: 使用统一的控制脚本来...

  • js流程控制

    2 - 流程控制 2.1 流程控制概念 2.2 顺序流程控制 ​ 顺序结构是程序中最简单、最基本的流程控制,它...

  • Thinuna SP-6216A 十六路电源时序器

    功能特点: 广播系统中控制多电源输出的必定选择 正确管理开关机顺序, 16路电源按顺序开启/关闭,每路接通时间间隔...

网友评论

      本文标题:多线的顺序控制

      本文链接:https://www.haomeiwen.com/subject/gajlsltx.html