使用Condition来实现,等四个线程处理结束了,再处理下一个输入,测试用例里有多个输入。
import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
static ReentrantLock lock = new ReentrantLock();
static Condition condition1 = lock.newCondition();
static Condition condition2 = lock.newCondition();
static Condition condition3 = lock.newCondition();
static Condition condition4 = lock.newCondition();
private static int count = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
int num = scanner.nextInt();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < num; i++) {
while (count % 4 != 0){
condition1.await();
}
System.out.print("A");
count++;
condition2.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < num; i++) {
while (count % 4 != 1){
condition2.await();
}
System.out.print("B");
count++;
condition3.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < num; i++) {
while (count % 4 != 2){
condition3.await();
}
System.out.print("C");
count++;
condition4.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread thread4 = new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < num; i++) {
while (count % 4 != 3){
condition4.await();
}
System.out.print("D");
count++;
condition1.signal();
}
System.out.println();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
thread1.start();
thread2.start();
thread3.start();
thread4.start();
try { thread4.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }
}