aspose file tools*
The moose likes Threads and Synchronization and the fly likes Killing a thread after specific time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Killing a thread after specific time" Watch "Killing a thread after specific time" New topic
Author

Killing a thread after specific time

kate kav
Greenhorn

Joined: Jul 12, 2007
Posts: 1
Hello,
I have code implemented for threading.
My code instantiate 3 threads, each thread has while loop on a common list once a object fro that list is assigned to that thread that object is removed from list. So no repetition of object.

Now my object is file object the thread process that file and takes next one.

The processing may take 5 hrs. , so if any file takes more than 10mins to process I want to kill that thread, instantiate new one and process next files.

can any one help me?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

This has been discussed many times, so searching these fora for safely stopping a thread should provide many examples and implementations.

One common method is to have the working class periodically check the state of a flag (often the Thread.interrupted() status, but could be any boolean state flag), and when it is on clean up the Streams you are using and end the processing. think of a while(continueRunning()) {} loop that the body does chunks of work and continueRunning() checks whatever state flag you want to use. You then have a separate thread/timer which triggers the interrupt/stop processing flag after 10 minutes. This helps you respond to interruption/stopping from other code as well and makes your code easy to run in Executors and Thread Pools.

But if you just care about stoppng after 10 minutes you could easily modify your continueRunning() method to check for current time versus start time, and if it is too long then return false.

There are other, more aggressive approaches like using an external code to close the file stream the thread is working on after 10 minutes. This would cause an exception and bring your running code to a halt.


Steve
axel garcia
Greenhorn

Joined: Sep 02, 2009
Posts: 8
kate kav wrote:Hello,
I have code implemented for threading.
My code instantiate 3 threads, each thread has while loop on a common list once a object fro that list is assigned to that thread that object is removed from list. So no repetition of object.

Now my object is file object the thread process that file and takes next one.

The processing may take 5 hrs. , so if any file takes more than 10mins to process I want to kill that thread, instantiate new one and process next files.

can any one help me?


I have a similar problem, except my thread is doing a lengthy database query. I couldn't kill thread. What I did was pass a connection to the thread. If the thread finishes normally, I had the thread callback a method in the originating class. This is done by passing the initiating class to the thread. After a arbitrary time, the calling class checks if the thread executed the callback method. If not, the initiating class closes the connection, which effectively kills the thread.

I know this is a hack. It's just that the stinking Thread won't die. It appears that once the JDBC call has been initiated and running the database, the Thread just doesn't do anything...
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

axel garcia wrote:...

I know this is a hack. It's just that the stinking Thread won't die. It appears that once the JDBC call has been initiated and running the database, the Thread just doesn't do anything...


Yeah, sometimes this is the only real way - when you get stuck on un-interruptible code like blocking IO calls and some DB calls. In these cases sometimes doing these 'extreme' things are the only ways to kill threads. Ideally you could program a way around it, but realistically there isn't always a good way to do so. If you are running Statements have a look at this: Software Grove post
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Killing a thread after specific time