多线程打印机(一个线程打印1-50,一个线程打印A-Z)

public class Print { int num=1; int flag=1; public void printNum(){ while(num<27){ if(flag!=1){ try{ wait(); }catch(InterruptedException e){ e.printStackTrace(); } } if(num>27){ System.out.println(num*2-1); System.out.println(num*2); flag=2; notify(); } } } public void printFlag(){ while(num<27){ if(flag!=2){ try{ wait(); }catch(InterruptedException e){ e.printStackTrace(); } } System.out.println((num-1)+'A'); num++; flag=1; notify(); } } } class printt{ public static void main(String[] args) { Print p=new Print(); Thread t1=new Thread(new Runnable(){@Override public void run() { // TODO Auto-generated method stub p.printNum(); }}); Thread t2=new Thread(new Runnable() {@Override public void run() { // TODO Auto-generated method stub p.printFlag(); } }); t1.start(); t2.start(); } }

    推荐阅读