1 回答
TA贡献1757条经验 获得超8个赞
首先你需要了解wait()是Object类的方法,所以它就像一个特定的线程在等待一些与Object相关的动作。因此,如果在 proHs 线程下调用 bidStep (int gt)并且您想停止 proHs 线程,基本上要等到选择特定按钮然后您需要将等待放在某个对象上,通常它应该是 Object需要采取一些行动。您需要在此处列出以下步骤:
proHs 对象引用。
锁定 proHs 对象。
调用 proHs.wait()。
从第二个线程您将执行以下操作: 1. 在 buttonClickListener 第二个线程内锁定 proHs 对象
。) 2. 调用 proHs.notify()。
class InterfaceImpl {
Thread proHs;
boolean btnResponse;
public boolean biddingStep(int gt) {
System.out.println(" ");
System.out.println("I HAVE OR NOT PART");
panelLicitace.setVisible(true);
mam.setVisible(true);
nemam.setVisible(true);
// HERE a code i want
//1. stop proHS thread
synchronized(proHs) {
proHs.wait();
//2. loop program, wait for input from 2 buttons
//3. return true or false
return btnResponse;
}
}
// This method should be called from another thread
public boolean btnClickListener() {
btnResponse = true or false
synchronized(proHs) {
proHs.notify();
}
}
}
这里 bidStep() 方法应该在 btnClickListener() 之前调用,这样一旦线程等待,另一个线程就会通知它。
添加回答
举报