从我的主程序调用{{1}}时,在线程中调用方法thread.start()
,如下所示:
run()
我需要在我的主类的现有线程中调用public class ServerThread implements Runnable {
public void run() {
//executable code
}
public void myMethod() {
//code to hopefully respond to a button press in main class.
}
}
。但据我了解,使用myMethod
是不可能的。还有其他方法吗?
答案 0 :(得分:3)
这应该有效:
ServerThread st = new ServerThread();
new Thread(st).start();
st.myMethod();
答案 1 :(得分:0)
修改你的课程:
public class ServerThread implements Runnable, ActionListener {
public void run() {
//executable code
}
void buttonPressed(ActionEvent ae){
// your event handling code
}
}
将ActionListener
和buttonPressed()
更改为特定于框架的侦听器类和事件处理程序。