package mutiThread;
public class alternatelyExecute {
private static Object LOCK = new Object();
private static boolean flag = false;
static Thread thread01 = new Thread() {
int num01 = 1;
int num02 = 2;
public void run() {
synchronized (LOCK) {
for (int i = 1; i <= 26; i++) {
System.out.print(num01);
System.out.print(num02);
num01 += 2;
num02 += 2;
if (!flag) {
flag = true;
LOCK.notify();
if (i < 26) {
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
};
static Thread thread02 = new Thread() {
int c = 65;
public void run() {
synchronized (LOCK) {
for (int i = 1; i <= 26; i++) {
System.out.print((char) c++);
System.out.print(" ");
if (flag) {
flag = false;
LOCK.notify();
if (i < 26) {
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
};
public static void main(String[] args) {
thread01.start();
thread02.start();
}
}
12A 34B 56C 78D 910E 1112F 1314G 1516H 1718I 1920J 2122K 2324L 2526M 2728N 2930O 3132P 3334Q 3536R 3738S 3940T 4142U 4344V 4546W 4748X 4950Y 5152Z