aspose file tools*
The moose likes Threads and Synchronization and the fly likes Simple Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Simple Thread Question" Watch "Simple Thread Question" New topic
Author

Simple Thread Question

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Aug 07, 2003
Posts: 1646
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Thread Question