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()
                    +" 过山洞");
    }
}

测试结果:



作业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);
			}
		}
	}
}

测试结果:




问题:作业二里线程二一直猜不对继续猜是,cpu的物理占用率提高了,如果不终止进程,一直猜下去,进程会不会崩溃?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值