• 多线程按顺序打印abc


    使用synchronized 实现

    synchronized原理

    public class ThreeThreads {
    
        public static void main(String[] args) throws InterruptedException {
            Object a = new Object();
            Object b = new Object();
            Object c = new Object();
    
            ThreadPrint threadPrint1 = new ThreadPrint("a", c,a);
            ThreadPrint threadPrint2 = new ThreadPrint("b", a,b);
            ThreadPrint threadPrint3 = new ThreadPrint("c", b,c);
    
    
            new Thread(threadPrint1).start();
            Thread.sleep(100);
            new Thread(threadPrint2).start();
            Thread.sleep(100);
            new Thread(threadPrint3).start();
            Thread.sleep(100);
    
        }
    
    
        public static class ThreadPrint implements Runnable{
            private String name;
            private Object prev;
            private Object self;
    
            public ThreadPrint(String name, Object prev, Object self) {
                this.name = name;
                this.prev = prev;
                this.self = self;
            }
    
            @Override
            public void run() {
    
                while (true){
                    synchronized (prev){
                        synchronized (self){
                            System.out.print(name);
                            self.notifyAll();
                        }
                        try{
                            prev.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    使用Lock 实现

    Lock原理
    ReentrantLock原理

    public class ThreeThreads {
    
        private static Lock lock = new ReentrantLock();
        private static int state = 0;
    
    
        static class ThreadA extends Thread {
            @Override
            public void run() {
                while (true) {
                    try {
                        lock.lock();
                        while (state % 3 == 0) {
                            System.out.print("a");
                            state++;
                        }
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    
        static class ThreadB extends Thread {
    
            @Override
            public void run() {
                while (true) {
                    try {
                        lock.lock();
                        while (state % 3 == 1) {
                            System.out.print("b");
                            state++;
                        }
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    
        static class ThreadC extends Thread {
    
            @Override
            public void run() {
                while (true) {
                    try {
                        lock.lock();
                        while (state % 3 == 2) {
                            System.out.print("c");
                            state++;
                        }
                    } finally {
                        lock.unlock();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            new ThreadA().start();
            new ThreadB().start();
            new ThreadC().start();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    使用Condition 实现

    Condition原理

    public class ThreeThreads {
    
        private static Lock lock = new ReentrantLock();
        private static Condition ca = lock.newCondition();
        private static Condition cb = lock.newCondition();
        private static Condition cc = lock.newCondition();
    
        private static int count = 0;
    
        static class ThreadA extends Thread{
    
            @Override
            public void run() {
                try {
                    lock.lock();
                    while (true){
                        while (count%3 != 0){
                            ca.await();
                        }
                        System.out.print("a");
                        count ++;
                        cb.signal();
                    }
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    
        static class ThreadB extends Thread{
    
            @Override
            public void run() {
                try {
                    lock.lock();
                    while (true){
                        while (count % 3 != 1){
                            cb.await();
                        }
                        System.out.print("b");
                        count++;
                        cc.signal();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    
        static class ThreadC extends Thread{
            @Override
            public void run() {
                try {
                    lock.lock();
                    while (true){
                        while (count % 3 != 2){
                            cc.await();
                        }
                        System.out.print("c");
                        count++;
                        ca.signal();
                    }
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    
        public static void main(String[] args) {
            new ThreadA().start();
            new ThreadB().start();
            new ThreadC().start();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    使用Semaphore实现

    Semaphore

     public class ThreeThreads {
        private static Semaphore sa = new Semaphore(1);
        private static Semaphore sb = new Semaphore(0);
        private static Semaphore sc = new Semaphore(0);
    
        static class ThreadA extends Thread{
            @Override
            public void run() {
                try {
                    while (true){
                        sa.acquire();
                        System.out.print("a");
                        sb.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        static class ThreadB extends Thread{
            @Override
            public void run() {
                try {
                    while (true){
                        sb.acquire();
                        System.out.print("b");
                        sc.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        static class ThreadC extends Thread{
            @Override
            public void run() {
                try {
                    while (true){
                        sc.acquire();
                        System.out.print("c");
                        sa.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) {
            new ThreadA().start();
            new ThreadB().start();
            new ThreadC().start();
        }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    【Spring Security】安全框架学习(十四)
    Python 博客园备份迁移脚本
    论文笔记: 图神经网络 GAT
    辅助驾驶功能开发-功能规范篇(24)-2-影子模式功能触发规范
    谈谈 Redis 数据类型底层的数据结构?
    IDEA2020创建JavaSE项目改造成JavaWeb项目并配置tomcat
    matlab运行CLBP程序为什么没有图?
    常用的设计模式
    华为OD 整数最小和(100分)【java】A卷+B卷
    用HarmonyOS ArkUI来开发一个购物应用程序
  • 原文地址:https://blog.csdn.net/weixin_39795049/article/details/132678350