import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.*;
public class Main{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
Object A = new Object();
Object B = new Object();
Object C = new Object();
Object D = new Object();
ExecutorService executor = Executors.newCachedThreadPool();
while((input = br.readLine()) != null) {
int num = Integer.parseInt(input);
executor.submit(new PrintStr(D, A, "A", num));
Thread.sleep(1);
executor.submit(new PrintStr(A, B, "B", num));
Thread.sleep(1);
executor.submit(new PrintStr(B, C, "C", num));
Thread.sleep(1);
Future<Integer> future = executor.submit(new PrintStr(C, D, "D", num));
Thread.sleep(1);
Integer f = future.get();
if(f == 0) {
System.out.println();
}
}
executor.shutdown();
}
}
/**
* 任务类
*/
class PrintStr implements Callable {
// 上个线程锁
public Object pre;
// 当前线程锁
public Object self;
// 当前要打印的字符
public String name;
// 打印次数
public Integer times;
public PrintStr(Object pre, Object self, String name, Integer times) {
this.pre = pre;
this.self = self;
this.name = name;
this.times = times;
}
@Override
public Integer call() {
// 多线程while约束线程执行次序
while(times > 0) {
synchronized (pre) {
synchronized (self) {
System.out.print(name);
times--;
self.notifyAll();
}
try {
if (times == 0) pre.notifyAll();
else pre.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return times;
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.*;
public class Main{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
Object A = new Object();
Object B = new Object();
Object C = new Object();
Object D = new Object();
ExecutorService executor = Executors.newCachedThreadPool();
while((input = br.readLine()) != null) {
int num = Integer.parseInt(input);
executor.submit(new PrintStr(D, A, "A", num));
Thread.sleep(1);
executor.submit(new PrintStr(A, B, "B", num));
Thread.sleep(1);
executor.submit(new PrintStr(B, C, "C", num));
Thread.sleep(1);
Future<Integer> future = executor.submit(new PrintStr(C, D, "D", num));
Thread.sleep(1);
Integer f = future.get();
if(f == 0) {
System.out.println();
}
}
executor.shutdown();
}
}
/**
* 任务类
*/
class PrintStr implements Callable {
// 上个线程锁
public Object pre;
// 当前线程锁
public Object self;
// 当前要打印的字符
public String name;
// 打印次数
public Integer times;
public PrintStr(Object pre, Object self, String name, Integer times) {
this.pre = pre;
this.self = self;
this.name = name;
this.times = times;
}
@Override
public Integer call() {
// 多线程while约束线程执行次序
while(times > 0) {
synchronized (pre) {
synchronized (self) {
System.out.print(name);
times--;
self.notifyAll();
}
try {
if (times == 0) pre.notifyAll();
else pre.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return times;
}
}