• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Thread Question

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have it working now, but I woudl liek to know why I see that printout every minute...

I was originally designing this thread to run every 5 minutes. However I realized that all it is really doing is continutally creatign threads and then forcign them to sleep for 5 minutes after they run. Why are threads being consitently created each minute instead of continually?

while (true)
{
Thread gl1 = new Thread(new test3(urlGL, username, password, loopCount));
gl1.start();

try
{
// Wait 5 minutes
Thread.currentThread().sleep(60000);
}

catch (InterruptedException e)
{ }

}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you have it coded, the thread running the while loop will create a new thread every minute. Why? Thread.sleep()'s argument is the number of milliseconds to sleep.

60000 ms = 60 s = 1 min

Instead, use a value of 300000. This is where constants really help out.
Originally posted by Anthony Smith:
I was originally designing this thread to run every 5 minutes. However I realized that all it is really doing is continutally creatign threads and then forcign them to sleep for 5 minutes after they run. Why are threads being consitently created each minute instead of continually?

The reason it runs every minute (or every five once you fix it) rather than continuously is that you are telling the current thread to sleep -- not the threads you are creating.

What would be the point of having a thread sleep for five minutes after it runs but before terminating? You're merely delaying the reclamation of resources. Perhaps you want to have the created thread to run every five minutes until it is stopped. In that case you should put a while loop in your run() method that first executes the work and then sleeps for five minutes (both inside the loop).
[ August 31, 2004: Message edited by: David Harkness ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic