• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Timeout for Thread

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.?
 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 7
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
pie. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic