我必须为他们开发一个程序和一个类,它们必须通过名为combinar(id)(id让我们区分氧和氢)的方法接收来自原子的请求。“同步”不起作用
必须通过前面提到的申请创建的分子只能在最终存在2个互补时才能生成。原子并没有一个(O case)或两个(H case)原子在等待(在这种情况下,新的也应该等待)。
我的问题是该消息的 “分子formada” 还创建,独立的价值观
public class Aire {
int atomosHidrogeno = 0;
int atomosOxigeno = 0;
int atomosH_esperando;
int atomosO_esperando;
public Aire(int atH, int atO2) {
this.atomosHidrogeno = atH;
this.atomosOxigeno = atO2;
this.atomosH_esperando = 0;
this.atomosO_esperando = 0;
}
public void combinar(int id) {
if (id == 0) // Hidrogeno
{
synchronized (this) {
while (this.atomosH_esperando == 2 || (this.atomosHidrogeno < 1 && this.atomosOxigeno < 0)) {
try {
wait();
this.atomosH_esperando++;
} catch (InterruptedException ie) {
}
}
notifyAll();
}
this.atomosH_esperando--;
System.out.println("Molécula formada");
} else if (id == 1) // Oxigeno
{
synchronized (this) {
while (this.atomosO_esperando == 1 || (this.atomosHidrogeno < 2)) {
try {
wait();
this.atomosO_esperando++;
} catch (InterruptedException ie) {
}
}
notifyAll();
}
this.atomosO_esperando--;
System.out.println("Molécula formada");
}
}
}
这是我使用的类的:
public class Principal {
public static void main(String[] args) {
Aire aire = new Aire(0, 0);
Atomo[] atomos_ = new Atomo[3];
Atomo[] atomos__ = new Atomo[3];
Atomo[] atomos___ = new Atomo[3];
for (int i = 0; i < 3; i++)
{
atomos_[i] = new Atomo(aire,0);
atomos_[i].start();
}
for (int i = 0; i < 3; i++)
{
atomos__[i] = new Atomo(aire,1);
atomos__[i].start();
}
for (int i = 0; i < 3; i++)
{
atomos___[i] = new Atomo(aire,0);
atomos___[i].start();
}
}
}
public class Atomo extends Thread {
private int id = 0;
Aire aire;
public Atomo(Aire aire, int id) {
this.id = id;
this.aire = aire;
}
public void run() {
this.aire.combinar(this.id);
}
}
请描述你期望你的代码行为如何,以及“*不起作用*”。 –
确实,消息是“Moléculaformada”,但我不打印,@fabian – garciacarmonaam
请注意,您正在递减同步块之外的'* _esperando'变量,该变量不是线程安全的。您可能想要将这些移动到同步块中。 –