天天看點

12.6第十五周JAVA作業

作業1:

        編寫多線程程式,模拟多個人通過一個山洞。這個山洞每次隻能通過一個人,每個人通過山洞的時間為2秒(sleep)。随機生成10個人,都要通過此山洞,用随機值對應的字元串表示人名,列印輸出每次通過山洞的人名。提示:利用線程同步機制,過山洞用一條輸出語句表示,該輸出語句列印輸出目前過山洞的人名,每個人過山洞對應一個線程,哪個線程執行這條輸出語句,就表示哪個人過山洞。

代碼實作:

package sci;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
public class Test1 {
    public static void main(String[] args) {
        String ary[] ={"Ashin","Monster","Stone","Masa","Ming","Mayday","Xin","Ran","May","Yan"};
        MyThread xx = new MyThread();
        Boolean flag=true;
        Set<Integer> set=new LinkedHashSet<Integer>();
        while(flag){
        if(set.size()==10){
        break;
        }
        int a=(int) (Math.random()*10);
        set.add(a);
        }
        for(int b:set){
        Thread th = new Thread(xx,ary[b]);
        th.start();
        }
	}
}
class MyThread implements Runnable {
    private static int wait=0;
    public void run() {
                wait=wait+2000;
                try
                {
                    Thread.sleep(wait);
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()
                    +" 過山洞");
    }
}
           

測試結果:

12.6第十五周JAVA作業

作業2:

        用兩個線程玩猜數字遊戲,第一個線程負責随機給出1~100之間的一個整數,第二個線程負責猜出這個數。要求每當第二個線程給出自己的猜測後,第一個線程都會提示“猜小了”、“猜大了”或“猜對了”。猜數之前,要求第二個線程要等待第一個線程設定好要猜測的數。第一個線程設定好猜測數之後,兩個線程還要互相等待,其原則是:第二個線程給出自己的猜測後,等待第一個線程給出的提示;第一個線程給出提示後,等待給第二個線程給出猜測,如此進行,直到第二個線程給出正确的猜測後,兩個線程進入死亡狀态。

代碼:

package sci;
import java.util.Random;
public class Guess {
	public static void main(String[] args) {
		ThreadOne one = new ThreadOne("線程 1");
		one.start();
		ThreadTwo two = new ThreadTwo("線程 2");
		two.start();
		try {
		Thread.sleep(1000);
		} catch (InterruptedException e) {
		e.printStackTrace();
		}
		while(true){
		if(two.getGuessResult().equals("猜對了")){
		break;
		}
	}
	}
}
class ThreadOne extends Thread{
	private String threadName;
	private static int theNumber;//存放要猜的數字
	public ThreadOne(String threadName) {
		this.threadName = threadName;
	}
	public void run() {
		Random random = new Random();
		theNumber = random.nextInt(100);
		System.out.println("出題線程出的題為:"+theNumber);
	}
//猜數字
	public static String guessNumber(int number){
		if(theNumber<number)
			return "big";
		else if(theNumber>number)
			return "little";
		else
			return "true";
	}
}
//猜題線程
class ThreadTwo extends Thread{
	private String threadName;
	private int minNum = 0;
	private int maxNum = 100;
	String guessResult = "";
	public ThreadTwo(String threadName) {
		this.threadName = threadName;
	}
	public String getGuessResult(){
		return guessResult;
	}
	public void run() {
		while(true){
			try {
				sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			int nowNum;
			Random random = new Random(); 
			nowNum=random.nextInt(100); 
			guessResult = ThreadOne.guessNumber(nowNum);
			if(guessResult.equals("big")){
				System.out.println(threadName+"結果是:"+nowNum+" 猜大了!");
			}else if(guessResult.equals("little")){
				System.out.println(threadName+"結果是:"+nowNum+" 猜小了!");
			}else{
				System.out.println(threadName+" 猜對了,結果是:"+nowNum);
			}
		}
	}
}
           

測試結果:

12.6第十五周JAVA作業

問題:作業二裡線程二一直猜不對繼續猜是,cpu的實體占用率提高了,如果不終止程序,一直猜下去,程序會不會崩潰?