• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a known bug with native threads.
http://www.blackdown.org/java-linux/jdk1.2-status/known-bugs.html
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This discussion is more apt in the 'Threads' forum. I am moving this thread to there.
Ajith
 
Bras cause cancer. And tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic