• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads and Listeners

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand how thread work, and I understand how listeners work.
But I get confused when I see listeners that are also threads.
(Code that implements the runnable interface and a listener interface.)
If the listener is listening for a certain action, the corresponding method for the action is called when the action occurs.
When is the run method called if the thread does nothing but sit and listen?
What will happen if the tread is asleep when the action occurs?
Will the run method ever try to run while one of the action methods is running?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Thread object (or a Runnable object) is just a plain old object. No magic about it. When the run() method of one of these is called, it's generally going to be called in its own thread. If any of the other methods are called, they're going to run in the thread that calls them.
So let's say you've got a Runnable, and its been handed to a Thread and start() has been called -- so its run() method is running.
Now let's also say that you've got a reference to that Runnable, and you want to call, say, toString() on it. No problem, you call that method. Does toString() run in the thread that run() is on? Nope, it runs in the caller's thread -- just like you'd expect.
OK, now, let's say that the Runnable is also an ActionListener, and it's registered with a JButton. Somebody presses the button, and actionPerformed() gets called on the Runnable/ActionListener. The actionPerformed method runs on the GUI event thread -- the fact that the Runnable/ActionListener's run() method is running on some other thread has nothing to do with it.
So the answers to your questions are
1) Never. If you don't give the Runnable to a Thread and call start(), run() won't get called.
2) Nothing special. The Runnable and ActionListener "personalities" are independent.
3) Yes, absolutely.
Generally, such an object would have some means of communicating between its " personalities" -- at the very least, a member variable that is changed by the event listener and read by the Runnable, so that an event occurring has some effect on how the run() method acts.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to pile an example on your final comment. I have a thread with a run method that loops while a member variable is true. (It's doing some background stuff until somebody closes a window.) The class also implements a listener for a shutdown event. When some object on another thread publishes the shutdown event, the listener method on my object sets the member variable to false. It does this on the publisher's thread. The next time the run method thread comes around the loop it sees the variable is false and exits the loop which ends the thread.
Hmm, I thought that would be clearer than it reads right now. Was it helpful?
 
reply
    Bookmark Topic Watch Topic
  • New Topic