• Post Reply Bookmark Topic Watch Topic
  • New Topic

Timeout for Thread  RSS feed

 
Imran Akbar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts,

is it possible to have a timeout for a thread, when it reaches that time it has to kill itself.?
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You can set up timeouts for most blocking operations, though (Object.wait, the await method provided by many classes under java.util.concurrent, acquire* methods also from classes under java.util.concurrent, poll and offer from BlockingQueue, etc.). You could also use getThreadCpuTime from the Threading MX bean to get the time used by the thread and exit if the limit has been exceeded.
Note that there's no way to (safely) terminate a thread that's blocked indefinitely, e.g. trying to acquire a monitor (entering a synchronized block).
 
Sachin Chandra
Greenhorn
Posts: 7
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In Java 5, CountDownLatch is there. You can pass it too a thread, and in the await method you can have the timeout for it.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html


SachinCR
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!