原书《Java面向对象程序设计》耿祥义 张跃平编著,习题15中的第11题,12题,13题
11.张某、李某和赵某买电影票,售票员只有三张5元的钱,电影票5元钱一张。张某拿20元一张的人民币排在李某的前面买票,李某排在赵的前面拿一张10元的人民币买票,赵某拿一张5元的人民币买票。
代码如下
package com.thread;
public class Question11 implements Runnable{
public SellTicketAgain sta;
Thread A,B,C;
public Question11() {
sta = new SellTicketAgain();
A = new Thread(this);
A.setName("张某");
B = new Thread(this);
B.setName("李某");
C = new Thread(this);
C.setName("赵某");
}
public static void main(String[] args) {
Question11 ans = new Question11();
ans.A.start();
ans.B.start();
ans.C.start();
}
@Override
public void run() {
if (Thread.currentThread()==A) {
sta.sell(20);
}
else if (Thread.currentThread()==B) {
sta.sell(10);
}
else if (Thread.currentThread()==C) {
sta.sell(5);
}
}
}
class SellTicketAgain{
int fiveNum=3,tweNum=0,tenNum=0;
public synchronized void sell(int money){
String name = Thread.currentThread().getName();
if(money==5){
fiveNum+=1;
System.out.println("给"+name+"入场劵,"+name+"的钱正好");
}
else if(money==10){
while (fiveNum<1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
tenNum+=1;
fiveNum-=1;
System.out.println("给"+name+"入场劵,"+name+"给10,我找5元");
}
else if(money==20){
while (fiveNum<3) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
tweNum+=1;
fiveNum-=3;
System.out.println("给"+name+"入场劵,"+name+"给20,我找15元");
}
notifyAll();
}
}
代码如下
package com.thread;
public class Question12 {
public static void main(String[] args) {
Person per = new Person();
per.student1.start();
per.student2.start();
per.teacher.start();
}
}
class Person implements Runnable{
Thread student1,student2,teacher;
Person(){
student1 = new Thread(this);
student1.setName("student1");
student2 = new Thread(this);
student2.setName("student2");
teacher = new Thread(this);
teacher.setName("teacher");
}
@Override
public void run() {
if (Thread.currentThread()==student1) {
try {
System.out.println(student1.getName()+"正在睡觉,不听课");
student1.sleep(10*60*1000);
} catch (InterruptedException e) {
System.out.println(student1.getName()+"被老师叫醒了");
student2.interrupt();
}
}
else if(Thread.currentThread()==student2){
try {
System.out.println(student2.getName()+"正在睡觉,不听课");
student2.sleep(60*60*1000);
} catch (InterruptedException e) {
System.out.println(student2.getName()+"被student1叫醒了");
}
}
else if(Thread.currentThread()==teacher){
for (int i = 0; i < 3; i++) {
System.out.println("上课");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
student1.interrupt();
}
}
}
13.要求在主线程中再创建三个线程:运货司机、装运工和仓库管理员。要求线程运货司机占有CPU资源后立刻联合线程装运工,也就是让运货司机一直等到装运工完成工作才能开车,而装运工占有CPU资源后立刻联合线程仓库管理员,也就是让装运工一直等到仓库管理员打开仓库才能开始搬运货物。
代码如下:
package com.thread;
public class Question15 {
public static void main(String[] args) {
People people = new People();
people.driver.start();
}
}
class People implements Runnable{
Thread admin,stevedore,driver;
People(){
admin = new Thread(this);
admin.setName("仓库管理员");
stevedore = new Thread(this);
stevedore.setName("装运工");
driver = new Thread(this);
driver.setName("运货司机");
}
@Override
public void run() {
if (Thread.currentThread()==driver) {
System.out.println(driver.getName()+"等"+stevedore.getName()+"装运货物");
try {
stevedore.start();
stevedore.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(driver.getName()+"开始开车");
}
else if(Thread.currentThread()==stevedore){
System.out.println(stevedore.getName()+"等"+admin.getName()+"开仓库");
try {
admin.start();
admin.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(stevedore.getName()+"开始装运货物");
}
else if(Thread.currentThread()==admin){
System.out.println(admin.getName()+"开始打开仓库,这需要三秒钟");
try {
admin.sleep(3*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("仓库已被"+admin.getName()+"打开");
}
}
}
如果文章有什么错误或者有什么建议,欢迎提出,大家共同交流,一起进步
文章转载请注明出处,请尊重知识产权