多线程交替打印数字和字母
数字从1打印到52, 字母从A打印到Z
例如 12A34B·······
package 酷家乐0913;public class A {static class Thread1 implements Runnable{private int num = 1;private int step = 1;private char c = 'A';@Overridepublic void run() {synchronized (this){ // 核心的地方,需要把这个类锁住while (step<=78){this.notify(); // 通知需要这个类的线程,开始竞争锁。if(step%3 ==0){System.out.print(c++);step++;}else {System.out.print(num++);step++;}try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}try {this.wait();//释放锁} catch (InterruptedException e) {e.printStackTrace();}}}}}public static void main(String[] args) {Thread1 t1 = new Thread1();new Thread(t1).start();new Thread(t1).start();}}
