温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在java中利用多线程有序输出ABC

发布时间:2021-03-09 17:38:37 来源:亿速云 阅读:206 作者:Leah 栏目:编程语言

本篇文章为大家展示了怎么在java中利用多线程有序输出ABC,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

方式1:利用synchronized

这种方式也就是使用java内置的monitor机制,配合wait和notifyAll,代码如下:

(1)利用volatile做线程间资源的同步访问,同时作为线程调度的标志;
(2)利用notifyAll来唤醒其他等待当前的monitor资源的线程;

public class ThreadOrderWithSync {

  private volatile int flag = 'A';
  private final static Object LOCK = new Object();

  Runnable a = () -> {
    while (true) {
      synchronized (LOCK) {
        if (flag == 'A' ) {
          System.out.println("A");

          flag = 'B';
          // let other thread race to get the monitor
          LOCK.notifyAll();
        } else {
          try {
            LOCK.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  };

  Runnable b = () -> {
    while (true) {
      synchronized (LOCK) {
        if (flag == 'B' ) {
          System.out.println("B");

          flag = 'C';
          // let other thread race to get the monitor
          LOCK.notifyAll();
        } else {
          try {
            LOCK.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  };

  Runnable c = () -> {
    while (true) {
      synchronized (LOCK) {
        if (flag == 'C' ) {
          System.out.println("C");

          flag = 'A';
          // let other thread race to get the monitor
          LOCK.notifyAll();
        } else {
          try {
            LOCK.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  };

  public void runTest() {
    Thread ta = new Thread(a);
    Thread tb = new Thread(b);
    Thread tc = new Thread(c);

    ta.start();
    tb.start();
    tc.start();
  }

  public static void main(String[] args) {
    ThreadOrderWithSync sync = new ThreadOrderWithSync();
    sync.runTest();
  }
}

方式2:利用并发包ReentrantLock和Condition的锁机制

上面方式1的synchronized机制,因为当前的所有线程都争用同一个monitor资源,因此只能通过notifyAll来通知其他线程来加锁,因此每次都会出现race condition,但是,通过ReentrantLock的Condition,我们可以精确控制,下一个该唤醒signal的线程是哪一个(因为我们知道执行的顺序是A->B->C的循环),相比synchronized的机制,Condition机制可以更精细化线程的调度设计,代码示例如下:

/**
 * @author xijin.zeng created on 2018/8/31
 * Thrads runing order: A->B->C
 */
public class ThreadOrderWithCondition {

  private static final ReentrantLock LOCK = new ReentrantLock();
  private static final Condition C_A = LOCK.newCondition();
  private static final Condition C_B = LOCK.newCondition();
  private static final Condition C_C = LOCK.newCondition();

  /**
   * init for A to run first
   */
  private volatile int flag = 'A';

  Runnable a = () -> {
    while (true) {
      LOCK.lock();

      if (flag == 'A') {
        System.out.println("A");
        flag = 'B';
        // signal B to run
        C_B.signal();
      } else {
        try {
          // block and wait signal to invoke
          C_A.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      LOCK.unlock();
    }
  };

  Runnable b = () -> {
    while (true) {
      LOCK.lock();

      if (flag == 'B') {
        System.out.println("B");
        flag = 'C';
        // signal C to run
        C_C.signal();
      } else {
        try {
          // block and wait signal to invoke
          C_B.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      LOCK.unlock();
    }
  };

  Runnable c = () -> {
    while (true) {
      LOCK.lock();

      if (flag == 'C') {
        System.out.println("C");
        flag = 'A';
        // signal A to run
        C_A.signal();
      } else {
        try {
          // block and wait signal to invoke
          C_C.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      LOCK.unlock();
    }
  };

  public void runTest() {
    Thread threadA = new Thread(a);
    Thread threadB = new Thread(b);
    Thread threadC = new Thread(c);

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

  public static void main(String[] args) {
    ThreadOrderWithCondition o = new ThreadOrderWithCondition();
    o.runTest();
  }
}

上述内容就是怎么在java中利用多线程有序输出ABC,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI