This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

telling when a thread has ended, without waiting for it

 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either sending an event (if there is more than one object that should get notified) or a callback interface would work.
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic