在运动中
在你有时装和缝纫机的地方制作一个程序,让操作员输入数据宽度和高度,通知缝纫机以执行他们的工作。
接收数据并Operator
处理并通知机器。 Machine
接收数据并完成该过程。
但是,当我运行时,Maquina
没有通知线程并且机器Operator
处于无限循环接收数据。
public class Operator extends Thread {
Scanner in = new Scanner(System.in);
int altura, largura;
public void run() {
while(true) {
synchronized (this) {
System.out.print("Altura: ");
altura = in.nextInt();
System.out.print("Largura: ");
largura = in.nextInt();
notify();
}
}
}
public String getForma() {
return "Forro de mesa : " + (altura * largura);
}
}
public class Maquina extends Thread{
private Operator c;
public Maquina(Operator c) {
this.c = c;
}
public void run() {
while(true) {
synchronized (c) {
try {
System.out.println("Waiting shape...");
c.wait();
System.out.println("init drawn...");
Thread.currentThread().sleep(3000);
System.out.println("drawing...");
Thread.currentThread().sleep(3000);
System.out.println(c.getForma() + ", finalized");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}