Java程序设计——多线程机制课后习题答案

Java程序设计——多线程机制课后习题答案

教材为:Java程序设计精编教程(第3版)

第12章——Java多线程机制

课后习题11:

张某、李某和赵某买电影票,售票员只有三张5元的钱,电影票5元钱一张。张某拿20元一张的人民币排在李某的前面买票,李某排在赵某的前面拿一张10元的人民币买票,赵某拿一张5元的人民币买票。


test11_Main.java

package test11;

public class test11_Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TicketHouse officer=new TicketHouse();
		Thread Zhang,Lee,Zhao;
		Zhang =new Thread(officer);
		Lee=new Thread(officer);
		Zhao=new Thread(officer);
		Zhang.setName("张某");
		Zhao.setName("赵某");
		Lee.setName("李某");
		Zhang.start();
		Lee.start();
		Zhao.start();
	}

}

TicketHouse.java

package test11;

public class TicketHouse implements Runnable{
	int fiveAmount=3;
	int tenAmount=0;
	int twentyAmount=0;
	public void run()
	{
		if(Thread.currentThread().getName().equals("张某"))
		{
			saleTicket(20);
		}
		else if(Thread.currentThread().getName().equals("李某"))
		{
			saleTicket(10);
		}
		else if(Thread.currentThread().getName().equals("赵某"))
		{
			saleTicket(5);
		}
	}
	private synchronized void saleTicket(int money)
	{
		if(money==20)
		{
			fiveAmount-=3;
			twentyAmount+=1;
			System.out.println(Thread.currentThread().getName()+"买票给了20元,找零15元");	
		}
		else if(money==10)
		{
			while(fiveAmount<1)
			{
				try
				{
					System.out.println(Thread.currentThread().getName()+"在一边等待....");
					wait();
					System.out.println("继续买票");
				}
				catch(InterruptedException e) {}
			}
			fiveAmount-=1;
			tenAmount+=1;
			System.out.println(Thread.currentThread().getName()+"买票给了10元,找零5元。");
		}
		else if(money==5)
		{
			fiveAmount+=1;
			System.out.println(Thread.currentThread().getName()+"买票的钱正好,给了5元,找零0元。");	
		}
		notifyAll();
	}
}

运行结果
在这里插入图片描述

课后习题12:

要求有三个线程:student1,student2和teacher,其中student1准备10分钟后再开始上课,student2准备睡一小时后再开始上课。teacher在输出三句“上课”后,吵醒休眠的线程student1;student1被吵醒后,负责再吵醒休眠的线程student2。


test12_Main.java

package test12;

public class test12_Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassRoom classroom=new ClassRoom();
		classroom.Teacher.setName("Tony老师");
		classroom.Student1.setName("甲同学");
		classroom.Student2.setName("乙同学");
		classroom.Student1.start();
		classroom.Student2.start();
		classroom.Teacher.start();
	}
}

ClassRoom.java

package test12;

public class ClassRoom implements Runnable{
	Thread Student1,Student2,Teacher;
	ClassRoom()
	{
		Teacher=new Thread(this);
		Student1=new Thread(this);
		Student2=new Thread(this);
	}
	public void run()
	{
		String name=Thread.currentThread().getName();
		if(name.equals("甲同学"))
		{
			try
			{ 
				System.out.println("我是"+name+",我在上课,想睡10分钟再开始上课");
//				System.out.println("想睡10分钟再开始上课");
				Thread.sleep(1000*10*60);
			}
			catch(InterruptedException e) 
			{
				System.out.println(name+"被Tony老师叫醒了");
			}
			Student2.interrupt();
			System.out.println(name+"继续上课");
		}
		else if(name.equals("乙同学"))
		{
			try
			{
				System.out.println("我是"+name+",我在上课,想睡1小时再开始上课");
				//System.out.println("想睡1小时再开始上课");
				Thread.sleep(1000*60*60);
			}
			catch(InterruptedException e)
			{
				System.out.println(name+"被甲同学叫醒了");
				Student2.interrupt();
			}
//			attachThread.interrupt();
			System.out.println(name+"继续上课");
		}
		else if(name.equals("Tony老师"))
		{
			for(int i=1;i<=3;i++)
			{
				System.out.println("上课!");
				try 
				{
					Thread.sleep(1000);
				}
				catch(InterruptedException e) {}
			}
//			attachThread.interrupt();
			Student1.interrupt();
		}
	}
}

运行结果
在这里插入图片描述

课后习题13:

要求在主线程中再创建三个线程:运货司机、装运工和仓库管理员。要求线程运货司机占有CPU资源后立刻联合线程装运工,也就是让运货司机一直等到装运工完成工作才能开车,而装运工占有CPU资源后立刻联合线程仓库管理员,也就是让装运工一直等到仓库管理员打开仓库才能开始搬运货物。


test13_Main.java

package test13;

public class test13_Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Squard squard=new Squard();
		squard.driver.start();
//		squard.shipper.start();
//		squard.stockman.start();

	}
}

Squard.java

package test13;

public class Squard implements Runnable{
	Thread shipper;//装运工
	Thread driver;//司机
	Thread stockman;//仓库管理员
	Squard()
	{
		shipper=new Thread(this);
		driver=new Thread(this);
		stockman=new Thread(this);
		shipper.setName("装运工");
		driver.setName("司机");
		stockman.setName("仓库管理员");
	}
	public void run()
	{
		if(Thread.currentThread().getName().equals("司机"))
		{
			System.out.println("I'm "+Thread.currentThread().getName()+",我在等装运工完成工作才能开车...");
			try
			{
				shipper.start();//线程开始
				shipper.join();//线程联合
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
			System.out.println("开始开车!");
		}
		else if(Thread.currentThread().getName().equals("装运工"))
		{
			System.out.println("I'm "+Thread.currentThread().getName()+",我在等仓库管理员...");
			try
			{
				stockman.start();//线程开始
				stockman.join();//线程联合
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
			System.out.println("开始装运货物!");
		}
		else if(Thread.currentThread().getName().equals("仓库管理员"))
		{
			System.out.println("I'm "+Thread.currentThread().getName()+",大家都在等我");
			System.out.println("打开仓库门需要5秒钟,5..4...3...2...1...0");
			try
			{
				stockman.sleep(5000);
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
			System.out.println("仓库门已打开!");
		}
	}
}


运行结果
在这里插入图片描述

  • 10
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值