package com.zhang.reflection.面试.多线程打印;
public class Synchronized实现 {
private int num;
private static final Object Lock = new Object();
private void print_ABC(int target) {
synchronized (Lock) {
//循环打印
for (int i = 0; i < 10; i++) {
while (num % 3 != target) {
try {
Lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num++;
System.out.print(Thread.currentThread().getName());
Lock.notifyAll();
}
}
}
public static void main(String[] args) {
Synchronized实现 wait_notify_abc = new Synchronized实现();
new Thread(() -> {
wait_notify_abc.print_ABC(0);
}, "A").start();
new Thread(() -> {
wait_notify_abc.print_ABC(1);
}, "B").start();
new Thread(() -> {
wait_notify_abc.print_ABC(2);
}, "C").start();
}
}