Skip to main content

(Not So) Stupid Questions 19: Remote Threaded Event Listener

November 1, 2007

{cs.r.title}







Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...." If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.

Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to

This may be a stupid question, but ... "Can I have a thread that doesn't use run()?"

First Thoughts

I have a simple problem--but am surprised I don't know how to answer it.

I have a class/object A running in one box that sends out messages/events. I write a second Java application, B, that runs on another box and wants to receive these messages/events from A--to do so, this B class has to implement an interface (call it A_Listener) of three methods (F1, F2, and F3) to receive these messages/events. But application B has lots of other things it has to do, other than just getting messages/events from A. In fact, its main work is something else, and its main method and other code does all this other stuff. Application B wants to receive A's messages/events asynchronously, letting the rest of its code do its usual work. So I wanted to write a Java thread that I could add to application B--to leave the rest of application B code the same, doing its main usual job--but have this extra thread in B receiving the messages and events from A.

But the only way I know how to write a Java thread is to mainly do the work of the thread in a run method. But the job that the thread has to do should be done in the three methods (F1, F2, and F3) that implement the A listener interface. How do you reconcile that with the fact that the work of the thread has to be done via the run method--while unfortunately, the reception of the messages and events has to be implemented by the explicit methods of the interface?

How can both the run method do the work of receiving the messages/events from A? (And the methods F1, F2, and F3 really do this, too.) The only way you can get this stuff from A is to get it in the methods F1, F2, and F3! I can't figure how to make a Java thread, with a run method, do this! Sorry to be repetitive--but I want to be clear what the problem is:

Can you write a thread in Java that doesn't use run, but uses other interface methods?


width="1" height="1" border="0" alt=" " />
Related Topics >> Programming   |