05-多线程

nobility 发布于 2021-03-30 00-常见算法 1170 次阅读


多线程

线程交替打印

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();
}
加油啊!即便没有转生到异世界,也要拿出真本事!!!\(`Δ’)/
最后更新于 2021-03-30