aspose file tools*
The moose likes Threads and Synchronization and the fly likes telling when a thread has ended, without waiting for it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "telling when a thread has ended, without waiting for it" Watch "telling when a thread has ended, without waiting for it" New topic
Author

telling when a thread has ended, without waiting for it

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

this is my first threading program so sorry if this question is stupid



i need to know when the thread finishes so i can remove the matchListener, but i don't want to hang everything up waiting (see code below) , or going to all the effort of starting a thread was pointless


so what is a usual tactic?

Thanks
p.s. and i just read a previous thread and am going back to work on my swing threads (edt was it?), from the MatchListener event handling
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Well, I'm not sure what you mean by "without waiting for it". If the thread's not done yet, and you want to know when it's done, you generally have to either (a) force it to complete right now, (b) travel through time, or (c) wait for it to end. I don't know a good way to do (a), or any way to do (b). But (c) is pretty doable. There are, however, much better ways to do it than the code you showed, so hopefully that's what you're asking about. Probably the best way given the setup you envision would be to simply call t.join(), where join() is a method defined in the Thread class. If that doesn't work for you, try reading about wait() and notify(), defined in Object. Does that help?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Couldn't the "run" method remove all match listeners from itself before it finishes? Or you could pass in a reference to the specific listener that should be removed.


Ping & DNS - my free Android networking tools app
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Ulf Dittmer wrote:Couldn't the "run" method remove all match listeners from itself before it finishes? Or you could pass in a reference to the specific listener that should be removed.

I like this idea, but there is other stuff as well (setting the search button to enabled, and the stop button to disabled).

last night (in bed) i was thinking maybe i could send a finished event, or set up a timer to check every 2 seconds.

Mike: by not waiting i meant not sitting in a loop checking the status every time round the loop.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Either sending an event (if there is more than one object that should get notified) or a callback interface would work.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Ulf Dittmer wrote:Either sending an event (if there is more than one object that should get notified) or a callback interface would work.


oh i haven't got a callback interface in yet, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: telling when a thread has ended, without waiting for it