《Java2实用教程》课后习题参考答案

内容发布更新时间 : 2024/5/29 17:27:48星期一 下面是文章的全部内容请认真阅读。

{

王小姐.售票规则(10); }

else if(Thread.currentThread()==赵某) {

王小姐.售票规则(5); } } }

class 售票员 {

int 五元钱个数=3,十元钱个数=0,二十元钱个数=0; String s=null;

public synchronized void 售票规则(int money) {

if(money==5) {

五元钱个数=五元钱个数+1; s=\给您入场卷您的钱正好\MyFrame.text.append(\}

else if(money==10) {

while(五元钱个数<1) { try{ wait(); }

catch(InterruptedException e){} }

五元钱个数=五元钱个数-1; 十元钱个数=十元钱个数+1;

s=\给您入场卷\您给我10 元,找您5 元\MyFrame.text.append(\}

else if(money==20) {

while((五元钱个数<4)&&(五元钱个数<1||十元钱个数<1))

{ try{ wait(); }

catch(InterruptedException e){} }

if(五元钱个数>=4) {

五元钱个数=五元钱个数-3; 二十元钱个数=二十元钱个数+1;

s=\给您入场卷\您给我20 元,找您15 元\MyFrame.text.append(\}

else if(五元钱个数>=1&&十元钱个数>=1) {

五元钱个数=五元钱个数-1; 十元钱个数=十元钱个数-1; 二十元钱个数=二十元钱个数+1;

s=\给您入场卷\您给我20 元,找您15 元\MyFrame.text.append(\} }

notifyAll(); } }

13.参照例子9.9,要求有3 个线程:student1、student 2 和teacher,其中student 1 准备睡1 0 分钟后再

开始上课,其中student 2 准备睡一小时后再开始上课。teache r 在输出3 句“上课”后,吵醒休眠的 线程student1;student 1 被吵醒后,负责再吵醒休眠的线程student2。 答: class InterruptSleep implements Runnable {

Thread student1,student2,teacher; InterruptSleep() {

teacher=new Thread(this); student1=new Thread(this); student2=new Thread(this); }

public void run() {

if(Thread.currentThread()==student2) { try{

System.out.println(\要睡一小时再听课,现在不听课\Thread.sleep(1000*60*60); }

catch(InterruptedException e) {

System.out.println(\还没睡够呢,但是被student1 给叫醒了\}

System.out.println(\开始上课\}

else if(Thread.currentThread()==student1) { try{

System.out.println(\要睡10 分钟再上课,现在不听课\Thread.sleep(1000*60*10); }

catch(InterruptedException e) {

System.out.println(\还没睡够呢,但是被teacher 给叫醒了\}

System.out.println(\开始上课\student2.interrupt(); }

else if(Thread.currentThread()==teacher) {

for(int i=1;i<=3;i++) {

System.out.println(\上课!\try{

Thread.sleep(500); }

catch(InterruptedException e){} }

student1.interrupt();

} } }

public class Test {

public static void main (String[] args) { InterruptSleep a=new InterruptSleep(); a.student2.start(); a.student1.start(); a.teacher.start(); } }

14.参照例子9.19,编写一个Jav a 应用程序,在主线程中再创建3 个线程:“运货司机”、“装运工”和“仓

库管理员”。要求线程“运货司机”占有CP U 资源后立刻联合线程“装运工”,也就是让“运货司机” 一直等到“装运工”完成工作才能开车,而“装运工” 占有CP U 资源后立刻联合线程“仓库管理员”, 也就是让“装运工” 一直等到“仓库管理员”打开仓库才能开始搬运货物。 答: public class Test {

public static void main(String args[]){ ThreadJoin a=new ThreadJoin(); a.运货司机.start(); a.装运工.start(); a.仓库管理员.start(); } }

class ThreadJoin implements Runnable {

Goods goods;

Thread 运货司机,装运工,仓库管理员; ThreadJoin() {

运货司机=new Thread(this); 装运工=new Thread(this); 仓库管理员=new Thread(this); 运货司机.setName(\运货司机\装运工.setName(\装运工\仓库管理员.setName(\仓库管理员\

}

public void run() {

if(Thread.currentThread()==运货司机) {

System.out.println(运货司机.getName()+\等\装运工.getName()+\装运货物\try{

装运工.join(); }

catch(InterruptedException e){}

System.out.println(运货司机.getName()+\开始运输\}

else if(Thread.currentThread()==装运工) {

System.out.println(装运工.getName()+\等\仓库管理员.getName()+\打开仓库\try{

仓库管理员.join(); }

catch(InterruptedException e){}

System.out.println(装运工.getName()+\开始搬运\}

else if(Thread.currentThread()==仓库管理员) {

System.out.println(仓库管理员.getName()+\准备打开仓库,请等待......\try{

仓库管理员.sleep(2000); }

catch(InterruptedException e){} goods=new Goods(\一批货物\

System.out.println(仓库管理员.getName()+\已经打开仓库,可以搬运了\} } }

class Goods {

String name; Goods(String name) {

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi