• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New Thread after every 2 minutes

 
Abid Rafique
Greenhorn
Posts: 10
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going by what you explain, I think you might be interested in the java.util.concurrent.ScheduledExecutorService
 
Abid Rafique
Greenhorn
Posts: 10
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. but your not starting the thread now (your just creating a temp object) or waiting for 2 minutes (5000?) or ...

(I give up ;-) )
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh no. another mistake.
it should be
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).



 
Abid Rafique
Greenhorn
Posts: 10
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic