【发布时间】:2022-01-11 01:31:33
【问题描述】:
该任务由三个吸烟者组成,它们是三个线程和一个名为 Agent 的类,它具有三个属性
烟草、打火机和纸。
吸烟者只有其中一件物品,代理类应该将两件物品随机放在桌子上,然后 Smoker 应该检查他们是否丢失了这两件物品并将它们捡起来以便他们可以用它们来吸烟。
我有一个问题,我的输出有点奇怪,吸烟者倾向于同时挑选他们丢失的所有物品,即使我认为我认为他们不允许这样做。
主类
public class Main {
public static void main(String[] args) {
Agent a = new Agent();
Pusac p1 = new Pusac(0,a,"Paper");
Pusac p2 = new Pusac(1,a,"Tobacco");
Pusac p3 = new Pusac(2,a,"Lighters");
p1.start();
p2.start();
p3.start();
}
}
我使用构造函数创建一个代理,然后将其传递给吸烟者的构造函数。每个吸烟者都有一个项目、ID 和代理。
吸烟类
public class Pusac extends Thread {
int id;
Agent a;
String owns;
public Pusac(int id, Agent a, String owns) {
this.id = id;
this.a = a;
this.owns = owns;
}
@Override
public void run() {
while(true) {
a.putTable();
if(this.owns.equals("Paper") && a.tobacco == true && a.lighters == true){
System.out.println("Smoker with ID " + this.id + " took tobacco and lighters.");
a.smoke();
} else if(this.owns.equals("Tobacco") && a.lighters == true && a.paper == true) {
System.out.println("Smoker with ID " + this.id + " took lighters and paper.");
a.smoke();
} else if(this.owns.equals("Lighters") && a.paper == true && a.tobacco == true) {
System.out.println("Smoker with ID " + this.id + " took paper and tobacco.");
a.smoke();
}
}
}
}
在吸烟者类函数中,我检查该代理是否有当前线程吸烟者所需的项目,然后我调用代理类中的函数。
代理类
public class Agent {
boolean paper;
boolean tobacco;
boolean lighters;
public void putTable() {
Random random = new Random();
int randomNumOne = random.nextInt(3);
switch(randomNumOne) {
case 0:
paper = true;
case 1:
tobacco = true;
case 2:
lighters = true;
}
int randomNumTwo = random.nextInt(3);
if(paper!=true && randomNumTwo==0) {
paper = true;
} else if(tobacco!=true && randomNumTwo == 1) {
tobacco = true;
} else if(lighters != true && randomNumTwo == 2) {
lighters = true;
}
}
public synchronized void smoke() {
System.out.println("Smoker with ID " + ((Pusac)Thread.currentThread()).id + " is smoking.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(((Pusac)Thread.currentThread()).owns.equals("Paper")) {
this.tobacco = false;
this.lighters = false;
System.out.println("Smoker with ID " + ((Pusac)Thread.currentThread()).id + " put down tobacco and lighters.");
} else if(((Pusac)Thread.currentThread()).owns.equals("Tobacco")) {
this.paper =false;
this.lighters =false;
System.out.println("Smoker with ID " + ((Pusac)Thread.currentThread()).id + " put down paper and lighters.");
} else if (((Pusac)Thread.currentThread()).owns.equals("Lighters")) {
this.tobacco =false;
this.paper =false;
System.out.println("Smoker with ID " + ((Pusac)Thread.currentThread()).id + " put down paper and tobacco.");
}
}
}
名为 Agent 的最后一个类由一个函数组成,该函数将两个项目随机放在桌子上 items are boolean 然后我有一个函数 smoke ,它只检查哪个吸烟者在吸烟,然后将所有内容设置为 假。
我得到的输出
Smoker with ID 2 took paper and tobacco.
Smoker with ID 1 took lighters and paper.
Smoker with ID 0 took tobacco and lighters.
Smoker with ID 2 is smoking.
Smoker with ID 2 put down paper and tobacco.
Smoker with ID 2 took paper and tobacco.
Smoker with ID 0 is smoking.
Smoker with ID 0 put down tobacco and lighters.
Smoker with ID 0 took tobacco and lighters.
Smoker with ID 1 is smoking.
这是错误的,因为实际上应该一次只选择一个人然后抽它,这样其他人也可以拥有它。
衷心感谢您的帮助。
【问题讨论】:
标签: java multithreading