在Java中使用线程时,是否需要runnable的替代方法

时间:2013-02-28 12:11:45

标签: java multithreading runnable

从我的主程序调用{​​{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是不可能的。还有其他方法吗?

2 个答案:

答案 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
    }

}

ActionListenerbuttonPressed()更改为特定于框架的侦听器类和事件处理程序。