交替打印0-100

synchronized

package juc;

public class Print100 {
    private static volatile int nums = 0;

    private static final  Object lock = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                // 获得锁
                synchronized (lock){
                    while (nums%2!=0){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName()+"  nums: "+nums++);
                    lock.notifyAll();
                }
            }
        });


        Thread threadB = new Thread(()->{
            for (int i = 0; i < 100; i++) {
                synchronized (lock){
                    while (nums%2!=1){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName()+"  nums: "+nums++);
                    lock.notifyAll();
                }

            }
        });

        threadA.start();
        threadB.start();
    }
}

Semaphore 实现

import java.util.concurrent.Semaphore;

public class PrintABC {
    private static int state = 0;

    // 三个信号量对象,分别表示A、B、C三个线程的初始许可数
    private static final Semaphore A = new Semaphore(1);
    private static final Semaphore B = new Semaphore(0);
    private static final Semaphore C = new Semaphore(0);

    public static void main(String[] args) {
        // 创建三个线程
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 循环100次
                    for (int i = 0; i < 100; i++) {
                        // 获取许可
                        A.acquire();
                        // 打印字母
                        System.out.println("A");
                        // 修改状态
                        state++;
                        // 释放许可
                        B.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 100; i++) {
                        B.acquire();
                        System.out.println("B");
                        state++;
                        C.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 100; i++) {
                        C.acquire();
                        System.out.println("C");
                        state++;
                        A.release();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 启动三个线程
        threadA.start();
        threadB.start();
        threadC.start();
    }
}

参考:https://juejin.cn/post/7252912234221387837?searchId=202308211040093299373C80F65B7FBFF0
https://blog.51cto.com/u_13544/6425126

作者:静默虚空
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

posted @   Chenyi_li  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示