• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic