05-多线程算法

nobility 发布于 2020-05-07 2640 次阅读


多线程

线程交替打印

public static int count = 0;
public static int max = 100;
 
public static void main(String[] args) {
  Object lock = new Object();
  Runnable runnable = () -> {
    synchronized (lock) {
      while (count < max) {
        System.out.println(Thread.currentThread().getName() + count++);
        lock.notify();
        if (count == max) return;
        lock.wait();
      }
    }
  };
  new Thread(runnable, "奇数").start();
  new Thread(runnable, "偶数").start();
}
此作者没有提供个人介绍
最后更新于 2020-05-07