Java2实⽤教程第5版第12章课后编程题参照例⼦8,模拟3个⼈排队买票…
Test类:
public class Test4_1 {
public static void main(String []args){
Ticket ticket=new Ticket();
Thread zhangmou=new Thread(ticket);
Thread limou=new Thread(ticket);
Thread zhaomou=new Thread(ticket);
zhangmou.setName("张某");
limou.setName("李某");
zhaomou.setName("赵某");
zhangmou.start();
limou.start();
zhaomou.start();
}
}
Tciket类:
public class Ticket implements Runnable{
int fiveAmount=3,tenAmount=0;
public synchronized void saleTicket(int money){
if(money==5){
fiveAmount+=1;
System.out.println("给"+Thread.currentThread().getName()+"票,"+Thread.currentThread().getName()+"钱正好");
}
else if(money==10){
while(fiveAmount<1){
try{
System.out.println(Thread.currentThread().getName()+"靠边等");
wait();
System.out.println(Thread.currentThread().getName()+"继续买票");
}
catch(InterruptedException e){}
}
tenAmount+=1;
fiveAmount-=1;
System.out.println("给"+Thread.currentThread().getName()+"⼀张票"+",零5块");
}
else if(money==20){
while(fiveAmount<3||fiveAmount<2&&tenAmount<1){
try{
System.out.println(Thread.currentThread().getName()+"靠边等");
wait();
System.out.println(Thread.currentThread().getName()+"继续买票");
}
catch(InterruptedException e){}
}
fiveAmount-=3;
System.out.println("给"+Thread.currentThread().getName()+"票"+",零15块");
}
notifyAll();
}
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);
}
}
}
/*额,总感觉哪⾥有点不对,就是saleTicket(20)的时候,那个判别条件,有10块之后,那⾥该怎么写呢tenAmount+=1;*/运⾏截图
参照例⼦6,要求有3个线程:student1…
Test类:
public class Test4_2 {
public static void main(String []args){
ClassRoom classRoom=new ClassRoom();
classRoom.student1.start();
classRoom.student2.start();
ClassRoom类:
public class ClassRoom implements Runnable {
Thread student1,student2,teacher;
ClassRoom(){
student1=new Thread(this);
student2=new Thread(this);
teacher=new Thread(this);
student1.setName("张三");
student2.setName("李四");
teacher.setName("王教授");
}
public void run(){
if(Thread.currentThread()==student1){
try{
System.out.Name()+"准备睡觉10分钟");
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.Name()+"被⽼师叫醒了");
}
System.out.Name()+"开始听课");
student2.interrupt();
}
else if(Thread.currentThread()==student2){
try{
System.out.Name()+"准备睡1个⼩时");
Thread.sleep(2000);
}catch(InterruptedException e){
System.out.Name()+"被"+Name()+"叫醒"); }
System.out.Name()+"开始听课");
}
else if(Thread.currentThread()==teacher){
for(int i=1;i<=3;i++)
System.out.println("上课");
try{
Thread.sleep(500);
}catch(InterruptedException e){}
student1.interrupt();
}
}
}
运⾏截图:
参照例⼦9,编写⼀个Java应⽤程序…
Test类
public class Test4_3 {
public static void main(String []args){
ThreadJoin a=new ThreadJoin();
Thread huochesiji=new Thread(a);
Thread zhuangyungong=new Thread(a);
Thread cangkuguanliyuan=new Thread(a);
huochesiji.setName("货车司机");
zhuangyungong.setName("装运⼯");
java怎么编写cangkuguanliyuan.setName("仓库管理员");
a.setThreadJoin1(zhuangyungong);
a.setThreadJoin2(cangkuguanliyuan);
huochesiji.start();
ThreadJoin类
public class ThreadJoin implements Runnable {
Thread joinThread1;
Thread joinThread2;
public void setThreadJoin1(Thread t1){
joinThread1=t1;//装运⼯
}
public void setThreadJoin2(Thread t2){
joinThread2=t2;//仓库管理员
}
public void run(){
if(Thread.currentThread().getName().equals("货车司机")){
System.out.println(Thread.currentThread().getName()+"等待"+Name()+"搬运货物上车"); try{
Thread.sleep(3000);
joinThread1.start();
joinThread1.join();
}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"拉⾛了10吨货物");
}
else if(Thread.currentThread()==joinThread1){
System.out.Name()+"等待仓库管理员开启仓库⼤门");
try{
Thread.sleep(4000);
joinThread2.start();
joinThread2.join();
}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"搬了10吨货物上车");
}
else if(Thread.currentThread()==joinThread2){
System.out.Name()+"正在开启仓库⼤门,请等待");
try{
Thread.sleep(5000);
}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"开启⼤门完毕");
}
}
}
运⾏截图:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论