aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to make  a Thread run for  particular amount of Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to make  a Thread run for  particular amount of Time " Watch "How to make  a Thread run for  particular amount of Time " New topic
Author

How to make a Thread run for particular amount of Time

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

I want to start a Thread , that actually runs for one minute and stops after it .


Please let me know how to achive this . Thanks .

Save India From Corruption - Anna Hazare.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

You can join on a thread for some time and after the join if it still is alive, interrupt to kill it.

Your thread must be able to handle the interruption that is going to occur.
This is one of the possible ways... may be someone else will come up with some more possibilities


OCPJP 6
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

You can join on a thread for some time


Thanks for your reply , But do this operation , there should be two threads .

Right now , a normal java class is calling this Thread.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Ravi Kiran V wrote:But do this operation , there should be two threads .

Right now , a normal java class is calling this Thread.

Non-sequitur?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Non-sequitur?


what do you mean by above ??
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Non_sequitur_(logic)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

To be honest, I come to the same conclusion.

The current thread starts this new thread. It should not block afterwards. However, this new thread needs to be limited in time. Using join on this thread from the current thread will block the current thread. To prevent that, the current thread starts two threads:
- the timed thread
- a thread that will call join on the timed thread


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Rob Prime wrote:To be honest, I come to the same conclusion.

The current thread starts this new thread. It should not block afterwards. However, this new thread needs to be limited in time. Using join on this thread from the current thread will block the current thread. To prevent that, the current thread starts two threads:
- the timed thread
- a thread that will call join on the timed thread

2 threads - thats really nice! Good one,Rob!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make a Thread run for particular amount of Time