• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pause and restart the Thread  RSS feed

 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to Threads and have some question.
I just want to make 1 background Thread and need to Pause and Restart the Thread.

Can you please tell me that how to pause and restart the Thread?

Thanks
Ram
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although java.lang.Thread has suspend and resume methods, these are deprecated. Check the JavaDocs for a discussion of why these are considered unsave.

Instead you might code the run() method the Thread is executing so that it sees a variable which can be set from your main program to control whether the Thread should sleep, continue execting or exit.

Bill
 
Jules Bach
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are only interested in pausing a thread, the sleep method does the trick,




However, this might not be want you have in mind - i have only used sleep for debugging, or to simulate 'work'.

If you want to pause a thread, because it needs to wait for some kind of event (like a resource becoming free, another thread finishing it's job etc..), you might be interested in semaphores, as well as wait and notify.

concurrency

Why do you want the thread to pause?
 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies... actually i want to insert one object in Collection and as soon as something is inserted in my collection, i have to notify the Thread and do the work as needed.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best best then is to use a shared BlockingQueue from the java.util.concurrent package. when you try to get an object out of theQueue you would pause the thread, until one is put in.

Read: http://java.sun.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html and http://java.sun.com/docs/books/tutorial/essential/concurrency/
 
Ram Chhabra
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

Thanks a lot for your reply, that would be very helpful for future but as of now i am using JDK1.5 and the nice solution provided by you is applicable in JDK 6.

Thanks for your reply
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is in J2SE 1.5.0 as well:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/BlockingQueue.html

And if you search there is a backport to older versions of Java as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!