写两个线程,一个线程打印 1~52,另一个线程打印A~Z,
打印顺序是12A34B...5152Z;
class Print{
private int i=1;
private char j='A';
public synchronized void printNumber(){
System.out.print(String.valueOf(i)+String.valueOf(i+1));
i=i+2;
notifyAll();
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
public synchronized void printChar(){
System.out.println(j);
j++;
notifyAll();
try{
if(j<='Z'){
wait();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
class PrintNumber implements Runnable{
private Print p;
public PrintNumber(Print p){
this.p=p;
}
public void run(){
for(int i=0;i<26;i++){
p.printNumber();
}
}
}
class PrintChar implements Runnable{
private Print p;
public PrintChar(Print p){
this.p=p;
}
public void run(){
for(int i=0;i<26;i++){
p.printChar();
}
}
}
public class test{
public static void main(String[]args){
Print p=new Print();
Thread printnumber=new Thread(new PrintNumber(p),"打印数字");
Thread printchar=new Thread(new PrintChar(p),"打印字母");
printnumber.start();
printchar.start();
}
}