Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes New Thread after every 2 minutes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "New Thread after every 2 minutes" Watch "New Thread after every 2 minutes" New topic
Author

New Thread after every 2 minutes

Abid Rafique
Greenhorn

Joined: Jan 09, 2012
Posts: 10

Hello Guys,
Its been really long since I last visited coderanch.
Glad to be back..

My Requirements is I want to start a new Thread after every 2 mnts.
for example

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

How about Thread.sleep(2*60*1000)?

However, please note that total waiting period might be more than 2*60*1000 milliseconds.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9930
    
159

Going by what you explain, I think you might be interested in the java.util.concurrent.ScheduledExecutorService

[My Blog] [JavaRanch Journal]
Abid Rafique
Greenhorn

Joined: Jan 09, 2012
Posts: 10

Thank you both of your for your help.
Since my requirement is somewhat different than regular interval execution, I plan to implement the following....


Thanks again guys.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

.. but your not starting the thread now (your just creating a temp object) or waiting for 2 minutes (5000?) or ...

(I give up ;-) )


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Also, 'waitfor10seconds' is not a good name for a method. How this : waitForTimeInterval(long milliseconds)?

Worse thing is, the name suggests that it waits for 10 seconds, but in reality, it sleeps only for 5 seconds.

Finally, a method meant to wait for particular time interval should only do waiting - no other stuff like creating thread etc.

I hope this helps.
Abid Rafique
Greenhorn

Joined: Jan 09, 2012
Posts: 10

I completely agree that I did not follow the conventions as those can be taken care later.

I m starting my thread first and then waiting for x number of seconds.

here is the method which i implemented...

Abid Rafique
Greenhorn

Joined: Jan 09, 2012
Posts: 10

oh no. another mistake.
it should be
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

So, if you get an exception, should it be okay to stop waiting the amount of time specified? I would generally think not - you either want to let the code that is waiting know (1) or continue waiting until the correct amount of time has passed (2).





Steve
Abid Rafique
Greenhorn

Joined: Jan 09, 2012
Posts: 10

Thanks Steve,
this certainly makes sense.

Though i dont expect the thread to be interrupted but its always better to have extra precaution. You never know..

Thanks again every one out here.

Trip to coderanch has always been fruitful and knowledgeful....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New Thread after every 2 minutes
 
Similar Threads
How to run a java application every 30 seconds?.
invokeAndWait()/invokeLater() ??
OracleConnectionCacheManager refresh problem
Thread Please help
Simple Thread Question