wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
could someone help me with this.
I want the following Thread class to print periodically "This is TestThread" ie every hour. When i interrupt the thread it should automatically print the message "This is TestThread". If i dont interrupt it should periodically send the message ie once in every hour. If interrupted, should keep track of the interrupt time and wait time. Then should periodically send the message exactly at the time remaining. So my question is how to keep track of the interrupted time and remaining wait time and how do i make the thread to send the message at exactly the same next periodic interval. I have the sample code below.
public class TestThread
implements Runnable
{
public static Thread myThread ;
public long waitTime = 1000*60*60;

public void run()
{
while (true)
{
long interruptTime = waitTime;
try
{
Thread.sleep(waitTime);
interruptTime -- ;
System.out.println("Time Remaining = " + interruptTime);
}
catch(InterruptedException e)
{
long nextTime = waitTime - interruptTime;
System.out.println("nextTime = " + nextTime);
}
System.out.println("This is TestThread");
}
}
public static void main(String[] args)
{
TestThread t = new TestThread();
myThread = new Thread(t);
myThread.start();
myThread.interrupt();
}
}
Thanks,
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
there is no way to garuntee it will print it every hour the only garuntee you have is that it will sleep for at least that amount of time but upon waking up it will be up to the thread scheduler to run the thread and you have no garuntee of that happening
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi, could anyone please further clarify me on this concept.
I have an infinite loop as shown above in the code, after the wait time has elapsed, the thread wakes up and prints the message and goes back to sleep and so on..., if i interrupt , i want this message to be printed and then want the thread to go back to sleep and send the message periodically ie the thread has to know at what time the next message to print.
I am able to print this message on and on...., but i don't know
what's happening if i interrupt the thread, does it print the message from the time it was interrupted or does the thread goes back to sleep for the wait time or does it sleep for the time left after it was interrupted.
Thanks,
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
Hi VCS, I'm not sure if this is what you're trying to do... I'll describe what the following does. The run method is a non-terminating loop that sleeps for some duration (I've set it to be 5 seconds) and then prints a message.
If it happens to be interrupted before those 5 seconds completes it will print a brief notification of such and then resume sleeping (with the intention of sleeping the remainder of the 5 seconds). The only problem is, something odd is going on after the thread is interrupted the second time. Any ideas?

[This message has been edited by Greg Whelan (edited June 05, 2000).]
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
After compiling and running the code with GCJ, it appears that the problem is with Sun's JVM (how often does one get to find one of those! )
Anyone else using running Classic VM (build 1.2.2-L, native threads, nojit) on Linux get the same results? (when the second interrupt is sent a barrage of interrupts are received by the loop).
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
It's a known bug with native threads.
http://www.blackdown.org/java-linux/jdk1.2-status/known-bugs.html
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
This discussion is more apt in the 'Threads' forum. I am moving this thread to there.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread
 
Similar Threads
isInterrupted() or interrupted() !!!!
Q on thread at Jxam
join()?
Code confusion
interrupt