交替打印三个线程

public class Demo {

    public static void main(String[] args) {
        ShareData sd = new ShareData();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
                sd.printA();
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
                sd.printB();
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
                sd.printC();
        }, "C").start();

    }

    static class ShareData {
        private static int num = 30;
        public synchronized void printA() {
            // 判断
            while (num % 3 != 0) {
                try {
                	// 进入锁池
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 干活
            num--;
            System.out.println(Thread.currentThread().getName());
            // 通知
            // 把锁池里的线程放入等待池
            this.notifyAll();
        }
        public synchronized void printB() {
            // 判断
            while (num % 3 != 2) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 干活
            num--;
            System.out.println(Thread.currentThread().getName());
            // 通知
            this.notifyAll();
        }
        public synchronized void printC() {
            // 判断
            while (num % 3 != 1) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 干活
            num--;
            System.out.println(Thread.currentThread().getName());
            // 通知
            this.notifyAll();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java三个线程交替打印可以使用经典的信号量机制来实现。首先,我们需要创建一个共享的信号量对象并初始化为1,用来保证三个线程的顺序执行。然后,我们创建一个打印任务类,实现Runnable接口,并重写run方法。在run方法中,我们使用信号量进行线程的控制。 具体的实现步骤如下: 1. 创建一个信号量对象,并初始化为1,用来控制线程的顺序执行。 Semaphore semaphore = new Semaphore(1); 2. 创建一个打印任务类,实现Runnable接口,并重写run方法。 class PrintTask implements Runnable { private String message; private Semaphore current; private Semaphore next; public PrintTask(String message, Semaphore current, Semaphore next) { this.message = message; this.current = current; this.next = next; } public void run() { try { for (int i = 0; i < 10; i++) { // 获取当前信号量的许可 current.acquire(); // 打印当前信息 System.out.print(message); // 释放下一个信号量的许可 next.release(); } } catch (InterruptedException e) { e.printStackTrace(); } } } 3. 创建三个打印任务对象,并指定当前信号量和下一个信号量的顺序。 Semaphore semaphoreA = new Semaphore(1); Semaphore semaphoreB = new Semaphore(0); Semaphore semaphoreC = new Semaphore(0); Runnable printTaskA = new PrintTask("A", semaphoreA, semaphoreB); Runnable printTaskB = new PrintTask("B", semaphoreB, semaphoreC); Runnable printTaskC = new PrintTask("C", semaphoreC, semaphoreA); 4. 创建三个线程,并分别启动这三个线程。 Thread threadA = new Thread(printTaskA); Thread threadB = new Thread(printTaskB); Thread threadC = new Thread(printTaskC); threadA.start(); threadB.start(); threadC.start(); 通过上述步骤,我们创建了三个信号量和三个打印任务,实现了三个线程交替打印。其中,Semaphore类的acquire方法用于获取一个许可,如果没有许可则阻塞,release方法用于释放一个许可。通过不同的信号量控制线程的顺序执行,从而实现了线程交替打印

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值