• Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens with thread in method when method executed again?

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Question: I have a JTree with Page objects. When a user rights click the page and chooses "Create Preview" and method is executed in another class. This method starts a thread. Another application will create previews etc so I have to wait for the previews to be done in the thread. If the user clicks create preview on the same page again I'll have to execute the method again. I wan't the thread that was allready looking for the previews to stop and create a new Thread. How can I stop the current Thread? When I run my code with a counter it doesn't break out of the while loop just creates a new Thread. The whole class is posted below but it is only the createPreview Method that is run from my JTree.


[ March 22, 2007: Message edited by: Mathias Nilsson ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you keep a reference to the thread and/or runnable that's off doing the work? Maybe member or instance variables. If the user tries to start a second one, you can attempt to halt the first one. Depending on what the first one is doing, it might not hear you right away.

You have to write halt(). Only you will know how it might stop the runnable.

Does that seem to fit?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your code, it doesnt look like your thread will do anything even after it wakes up from sleep. Typically, you would need a thread to sleep only if it does something after getting up!!
May be i am missing something.
Yeah, generally, answering your question, you can make the thread stop by doing exactly what Stan has told. You need to make a thread to sleep and after waking up it should check for a flag like "isHalted" and if it is false it can execute again else exit. This does not insure that as soon as you set the isHalted flag to true, the thread will stop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!