11)第一个程序:LeftAndRight.javapublicclassLeftAndRight{publicstaticvoidmain(Stringargs[]){Lefthandleft;//声明线程类对象Righthandright;left=newLefthand();//新建线程。right=newRighthand();left.start();//线程开始运行后,Lefthand类中的run方法将被执行。right.start();for(inti=0;i=6;i++){System.out.println(我是主线程);}}}classLefthandextendsThread{publicvoidrun(){for(inti=1;i=9;i++){System.out.println(我是左手线程);try{//线程执行一次输出后,休眠500ms(毫秒),使自己放弃CPU资源,//500ms时间到后,重新排队等待CPU的调度sleep(500);}catch(InterruptedExceptione){//如果线程在休眠期被打断,将抛出InterruptedException,//所以sleep方法需要在try……catch语句块中被调用}}}}classRighthandextendsThread{publicvoidrun(){for(inti=1;i=5;i++){System.out.println(我是右手线程);try{sleep(300);}catch(InterruptedExceptione){}}}}2)第二个程序:StudentAndTeacher.javapublicclassStudentAndTeacher{/***两个线程共享一个对象ComputerSumsum,在运行的过程中修改这个对象的成员变量sum。2*为了使结果尽量不依赖于当前CPU资源的使用情况,应当让线程主动调用sleep方法,让出CPU的控制权进入中断状态*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubComputerSumsum=newComputerSum();Peopleteacher=newPeople(教师,100,sum);Peoplestudent=newPeople(学生,100,sum);teacher.start();student.start();}}classComputerSum{//线程共享的对象类ComputerSum,其中定义了一个数据成员sumintsum=0;publicvoidsetSum(intn){sum=n;}publicintgetSum(){returnsum;}}classPeopleextendsThread{//类People是Thread的子类ComputerSumcomputerSum;inttimeLength;People(Strings,inttimeLength,ComputerSumsum){//构造方法setName(s);//设置Thread的名称this.timeLength=timeLength;//设置休眠时间this.computerSum=sum;//设置ComputerSum类的对象sum}publicvoidrun(){//重写Thread父类的run()方法for(inti=1;i=5;i++){intm=computerSum.getSum();//取出computerSum对象中的数据成员sumcomputerSum.setSum(m+1);//将数据成员sum的值加1System.out.println(我是+getName()+,现在的和是+computerSum.getSum());//输出当前处于运行中的线程的信息try{sleep(timeLength);//线程休眠timeLength毫秒}catch(InterruptedExceptione){3}}}}3)第三个程序:StudentAndTeacher1.javapublicclassStudentAndTeacher1{/***StudentAndTeacher1为StudentAndTeacher的改进版本*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubComputerSumsum=newComputerSum();People1people=newPeople1(200,sum);people.teacher.start();people.student.start();}}classComputerSum1{//线程共享的对象类ComputerSum,其中定义了一个数据成员sumintsum=0;publicvoidsetSum(intn){sum=n;}publicintgetSum(){returnsum;}}classPeople1implementsRunnable{//类People是Thread的子类ComputerSumcomputerSum;inttimeLength;Threadteacher,student;People1(inttimeLength,ComputerSumsum){//构造方法teacher=newThread(this);//类People1实现了接口Runnableteacher.setName(教师);student=newThread(this);student.setName(学生);this.timeLength=timeLength;//设置休眠时间this.computerSum=sum;//设置ComputerSum类的对象sum4}publicsynchronizedvoidcomputer(Threadthread){intm=computerSum.getSum();//取出computerSum对象中的数据成员sumcomputerSum.setSum(m+1);//将数据成员sum的值加1System.out.println(我是+thread.getName()+,现在的和是+computerSum.getSum());}publicvoidrun(){//重写Thread父类的run()方法for(inti=1;i=5;i++){if(Thread.currentThread()==teacher){computer(teacher);}elseif(Thread.currentThread()==student){computer(student);}try{Thread.sleep(timeLength);//线程休眠timeLength毫秒}catch(InterruptedExceptione){}}}}4)第四个程序:GuessNumberGame.java/*猜数程序第一个线程givenNumberThread负责随机给出1-100之间的一个整数第二个线程guessNumberThread负责猜出这个数每当第二个线程给出自己的猜测后第一个线程会将其比较后,提示“猜大了”,“猜小了”,“猜对了”*/publicclassGuessNumberGame{publicstaticvoidmain(String[]args){GuessNumbergn=newGuessNumber();gn.givenNumberThread.start();//启动线程gn.guessNumberThread.start();}}classGuessNumberimplementsRunnable{intrealNumber,guessNumber,min=0,max=100,message;//realNumber:随机数;guessNumber:某次猜得的数;message:提示信息finalintSMALLER=-1,LARGER=1,SUCCESS=8;5ThreadguessNumberThread,givenNumberThread;//guessNumberThread:线程,给出随机整数并判定线程givenNumberThread猜得的数字是否正确GuessNumber(){guessNumberThread=newThread(this);givenNumberThread=newThread(this);}publicvoidrun(){for(inti=1;true;i++){if(Thread.currentThread()==givenNumberThread){//如果当前正在运行的线程是givenNumberThreadif(i==1){//givenNumberThread线程第一次运行时,给出一个随机数realNumber=(int)(Math.random()*100)+1;System.out.println(给你一个随机数,猜猜是多少?realNumber=+realNumber);}else{/*比较guessNumber和realNumber的大小,并向猜数线程guessNumberThread传递猜大了或猜小了等信息*/if(guessNumberrealNumber){//猜得的数偏大message=LARGER;System.out.println(您猜的数偏大);}elseif(guessNumberrealNumber){//猜得的数偏小message=SMALLER;System.out.println(您猜的数偏小);}elseif(guessNumber==realNumber){//猜对了,guessNumber==realNumbermessage=SUCCESS;System.out.println(恭喜,您猜对了!);return;//猜对了,givenNumberThread运行结束,返回}}//elsetry{Thread.sleep(1500);//休眠1000毫秒,让出CPU资源给其他线程}catch(InterruptedExceptione){}}//ifif(Thread.currentThread()==guessNumberThread){//如果当前正在运行的线程是guessNumberThreadif(i==1){/*guessNumberThread线程第一次运行时,guessNumber=(min+max)/2=(0+100)/2=506后面guessNumberThread采用“折半”查找的原理根据每次从guessNumberThread那里得到的提示信息,修改max或min的值,给出新的guessNumber的取值*/guessNumber=(min+max)/2;System.out.println(我猜这个数是+guessNumber);}else{if(message==SMALLER){min=guessNumber;guessNumber=(min+max)/2;System.out.println(我猜这个数是+guessNumber);}elseif(message==LARGER){max=guessNumber;guessNumber=(min+max)/2;System.out.println(我猜这个数是+guessNumber);}elseif(message==SUCCESS){//根据message得知猜对了,guessNumberThread运行结束,返回System.out.println(我成功了!!!);return;}}//elsetry{Thread.sleep(1500);//休眠1000毫秒,让出CPU资源给其他线程}catch(Interrup