标题 JAVA多线程实现控制台打印A1B2C3…Z26
public class Test4 {
public static String chars = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
static Thread t1 = null;
static Thread t2 = null;
public static void main(String[] args) {
final Object lock = new Object();
t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("t1 开始");
lock.notify();
char[] c = chars.toCharArray();
for (int i = 0; i < c.length; i++) {
try {
System.out.println("t1="+c[i]);
lock.wait();
lock.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t1 结束");
}
}
},"t1");
t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("t2 开始");
lock.notify();
for (int i = 1; i <= 26; i++) {
try {
System.out.println("t2="+i);
lock.wait();
lock.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t2 结束");
}
}
},"t2");
t1.start();
t2.start();
}
}