我试图从另一个班级调用我的通知我的线程,但我认为我做错了。我已经阅读了许多尝试类似事情的人的例子,但我似乎无法将它应用到我的代码中。我认为我的问题是我的同步对象,但我不确定。我是线程新手,所以不要羞辱我。这是我的代码示例:
public class SendTextOffEdt implements Runnable {
private static final long SLEEP_TIME = 3000;
public static String TEXT = "Sent off of EDT\n";
private TextBox myTextBox;
//public boolean waitBoolean = false;
public SendTextOffEdt(TextBox myTextBox) {
this.myTextBox = myTextBox;
}
@Override
public void run() {
synchronized(this){
//while (true) {
try {
myTextBox.appendTextOffEdt(TEXT);
wait();
} catch (InterruptedException e) { //******** i changed this from another exception!!
System.out.println("*----------thread interrupted!");
myTextBox.appendTextOffEdt(TEXT);
}
}
}
}
public class Combat extends JPanel {
private SendTextOffEdt sendTextOffEdt = new SendTextOffEdt(textbox); //just added this object
public Combat(){
TestNotify();
}
public void TestNotify(){
synchronized(sendTextOffEdt){ //added the sendTextOffEdt object here
sendTextOffEdt.notifyAll();
System.out.println("has been notified");
}
}
}