# 三个线程轮流打印ABC,打印N次

# synchronized锁同步

public void print(String str, int target) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        synchronized (obj) {
            while (state % 3 != target) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            state++;
            System.out.print(str);
            obj.notifyAll();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# lock锁同步

public void print(String str, int target) throws InterruptedException {
    for (int i = 0; i < n; ) {
        try {
            lock.lock();
            while (state % 3 == target) {
                System.out.print(str);
                state++;
                i++;
            }
        } finally {
            lock.unlock();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Last Updated: 4/9/2024