线程调度之多线程循环打印ABCABC

参考:

https://blog.csdn.net/qq_32655383/article/details/51660925

但是我有点看不明白,于是就自己改写了下,后面分析。
线程类:

package threadabc;

public class ThreadPrint implements Runnable {  //A

    private Object after;  // Object b 下一个要执行的对象锁
    private Object now;    //Object a 当前对象锁
    private String name;

    public ThreadPrint(String name, Object after, Object now) {
        this.name = name;
        this.now = now;
        this.after = after;
    }

    @Override
    public void run() {
       for (int i=0;i<100;i++){
           System.out.println(name);   //A
           synchronized (now) {
                   synchronized (after) {
                       after.notify();   //B
                   }
               try {
                   now.wait();      //线程A放入a锁
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       }
    }
}

Main主函数:

 public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        ThreadPrint threadPrintA = new ThreadPrint("A",b,a);
        ThreadPrint threadPrintB = new ThreadPrint("B",c,b);
        ThreadPrint threadPrintC = new ThreadPrint("C",a,c);
        Thread threadA = new Thread(threadPrintA);
        Thread threadB = new Thread(threadPrintB);
        Thread threadC = new Thread(threadPrintC);
            threadA.start();
            threadA.sleep(100);
            threadB.start();
            threadB.sleep(100);
            threadC.start();
    }

关键:

a对象锁对应A线程
b对象锁对应B线程
c对象锁对应C线程

流程:
1. 执行A线程:
a对象,b对象传入A线程
当A线程执行完成后,将A线程关入a对象锁中,并且从b对象锁中唤醒B线程,因为b锁中还没有B线程所以没用。

  1. 因为按照主线程顺序,接下来执行线程B
    b对象,c对象传入A线程

当B线程执行完成后,将B线程关入b对象锁中,并且从c对象锁中唤醒C线程,因为c对象锁中还没有C线程所以没用。

  1. 因为按照主线程顺序,接下来执行线程C
    c对象,a对象传入A线程

当C线程执行完成后,将C线程关入c对象锁中,并且从a对象锁中唤醒A线程。此时只有A线程执行任务。

由此一个循环结束,开启重复循环

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的Semaphore信号量实现,并且基于线程间协作的方式来解决: ```java import java.util.concurrent.Semaphore; class PrintThread extends Thread { private Semaphore currentSemaphore; private Semaphore nextSemaphore; private String message; private int repeatTimes; public PrintThread(Semaphore currentSemaphore, Semaphore nextSemaphore, String message, int repeatTimes) { this.currentSemaphore = currentSemaphore; this.nextSemaphore = nextSemaphore; this.message = message; this.repeatTimes = repeatTimes; } public void run() { try { for (int i = 0; i < repeatTimes; i++) { currentSemaphore.acquire(); System.out.print(message); nextSemaphore.release(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { Semaphore semA = new Semaphore(1); Semaphore semB = new Semaphore(0); Semaphore semC = new Semaphore(0); int repeatTimes = 10; PrintThread threadA = new PrintThread(semA, semB, "A", repeatTimes); PrintThread threadB = new PrintThread(semB, semC, "B", repeatTimes); PrintThread threadC = new PrintThread(semC, semA, "C", repeatTimes); threadA.start(); threadB.start(); threadC.start(); } } ``` 上述代码中,我们使用三个Semaphore对象控制三个线程的执行顺序。具体来讲,我们将Semaphore semA、Semaphore semB和Semaphore semC初始化分别为1、0和0,因为我们希望线程A最先执行。然后,我们创建三个PrintThread对象,并分别将Semaphore对象作为参数传入,同时设置循环次数为10。 在PrintThread的run()方法中,我们使用Semaphore对象实现线程间的等待和释放。首先,currentSemaphore.acquire()会在当前Semaphore的值减1后继续执行,因为我们将Semaphore semA初始化为1,所以线程A会直接执行。然后,线程会打印出自己的输出(即message),并调用nextSemaphore.release()方法,将下一个Semaphore的值加1,这里即将Semaphore semB的值加1。接着线程继续执行循环,等待当前Semaphore的值变为1后再次执行,直到达到循环次数就结束。 通过上述方法,我们能够实现三个线程的有序输出,且可以重复执行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值