多线程

来源:互联网 发布:java开发是做什么的 编辑:程序博客网 时间:2024/06/10 06:26
作业1:

         编写多线程程序,模拟多个人通过一个山洞。这个山洞每次只能通过一个人,每个人通过山洞的时间为2秒(sleep)。随机生成10个人,都要通过此山洞,用随机值对应的字符串表示人名,打印输出每次通过山洞的人名。提示:利用线程同步机制,过山洞用一条输出语句表示,该输出语句打印输出当前过山洞的人名,每个人过山洞对应一个线程,哪个线程执行这条输出语句,就表示哪个人过山洞。


 class Though implements Runnable {
private Object though=new Object();
public void run(){
synchronized(though){
System.out.println(Thread.currentThread().getName()+"通过山洞");
}
try{
Thread.sleep(2);
 }catch(InterruptedException e){
 e.printStackTrace();
 }
 }
}



public class Test2 {
public static void main(String[] args) {
Though t=new Though();
for(int i=0;i<10;i++){
new Thread(t).start();
}
}


}








作业2:
         用两个线程玩猜数字游戏,第一个线程负责随机给出1~100之间的一个整数,第二个线程负责猜出这个数。要求每当第二个线程给出自己的猜测后,第一个线程都会提示“猜小了”、“猜大了”或“猜对了”。猜数之前,要求第二个线程要等待第一个线程设置好要猜测的数。第一个线程设置好猜测数之后,两个线程还要相互等待,其原则是:第二个线程给出自己的猜测后,等待第一个线程给出的提示;第一个线程给出提示后,等待给第二个线程给出猜测,如此进行,直到第二个线程给出正确的猜测后,两个线程进入死亡状态。




public class NumberTest {
public static void main(String[] args) {
Number number=new Number();// TODO Auto-generated method stub
number.giveNumberThread.start();
number.guessNumberThread.start();
}
}
class Number implements Runnable {
  final int SMALLER=-1,LARGER=1,SUCCESS=8;
  int realNumber,guessNumber,min=0,max=100,message=SMALLER;
  boolean pleaseGuess=false,isGiveNumber=false;
  Thread giveNumberThread,guessNumberThread;
  Number() {
  giveNumberThread=new Thread(this); 
  guessNumberThread=new Thread(this);
  }
  public void run() {
     for(int count=1;true;count++) {
        setMessage(count);   
        if( message==SUCCESS)
           return;
     }
  }
  public synchronized void setMessage(int count) {
     if(Thread.currentThread()==giveNumberThread&&isGiveNumber==false) {
         realNumber=(int)(Math.random()*100)+1;
         System.out.println("随机给你一个1至100之间的数,猜猜是多少?");
         isGiveNumber=true;
         pleaseGuess=true;
     }
     if(Thread.currentThread()==giveNumberThread) {
         while(pleaseGuess==true)             
            try  { wait();  
            }
            catch(InterruptedException e){}
            if(realNumber>guessNumber)  { 
               message=SMALLER;
               System.out.println("你猜小了");
            }
            else if(realNumber<guessNumber) {
               message=LARGER;
               System.out.println("你猜大了");
            } 
            else {
               message=SUCCESS;
               System.out.println("恭喜,你猜对了");
            }  
            pleaseGuess=true;
         }
     if(Thread.currentThread()==guessNumberThread&&isGiveNumber==true) {
            while(pleaseGuess==false)
               try { wait();  
               }
               catch(InterruptedException e){}
               if(message==SMALLER) {
                  min=guessNumber;
                  guessNumber=(min+max)/2; 
                  System.out.println("我第"+count+"次猜这个数是:"+guessNumber);
               }
               else if(message==LARGER) {
                  max=guessNumber;
                  guessNumber=(min+max)/2; 
                  System.out.println("我第"+count+"次猜这个数是:"+guessNumber);
               }
               pleaseGuess=false; 
     }
     notifyAll();
  }
}








import java.util.Random;


public class ThreadTest {


public static void main(String[] args) {
MyThread mt=new MyThread();
Random random=new Random();// TODO Auto-generated method stub
for(int i=1;i<=10;i++)
{
int a=random.nextInt(100);
new Thread(mt,""+a).start();
}
}
}
class MyThread implements Runnable{
Object lock=new Object();
public void run()
{
synchronized(this){
System.out.println(Thread.currentThread().getName()+"正在过山洞");
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}