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,