参照例子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.teacher.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.println(student1.getName()+"准备睡觉10分钟");
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(student1.getName()+"被老师叫醒了");
}
System.out.println(student1.getName()+"开始听课");
student2.interrupt();
}
else if(Thread.currentThread()==student2){
try{
System.out.println(student2.getName()+"准备睡1个小时");
Thread.sleep(2000);
}catch(InterruptedException e){
System.out.println(student2.getName()+"被"+student1.getName()+"叫醒");
}
System.out.println(student2.getName()+"开始听课");
}
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("装运工");
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()+"等待"+joinThread1.getName()+"搬运货物上车");
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.println(joinThread1.getName()+"等待仓库管理员开启仓库大门");
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.println(joinThread2.getName()+"正在开启仓库大门,请等待");
try{
Thread.sleep(5000);
}catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"开启大门完毕");
}
}
}
运行截图: