import java.util.concurrent.locks.ReentrantLock;
public class PrintTest {
private static ReentrantLock lock = new ReentrantLock();
private static int state = 0;
static class ThreadA extends Thread {
@Override
public void run() {
for (int i = 0; i < 26; ) {
lock.lock();
try {
while (state % 2 == 1) {
state++;
System.out.print((char)('A'+i));
i++;
}
} catch (Exception e) {
throw e;
} finally {
lock.unlock();
}
}
}
}
static class ThreadB extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; ) {
lock.lock();
try {
while (state % 2 == 0) {
state++;
System.out.print(i+1);
i++;
}
} catch (Exception e) {
throw e;
} finally {
lock.unlock();
}
}
}
}
public static void main(String[] args) {
ThreadA threadA = new ThreadA();
threadA.start();
ThreadB threadB = new ThreadB();
threadB.start();
}
}