Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Thread Sleeping problem  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
public class test extends Thread
{
public void run()
{
try{
for(int i=0;i<5;i++)
System.out.println(Thread.currentThread() + " " + i);
Thread.sleep(10000);
}
catch(Exception e){}
}

public static void main(String arg[]) throws Exception
{
Thread t=new Thread(new test(),"joe");
Thread t1=new Thread(new test(),"jo");
t.start();
t1.start();
}}

The problem in this code is that the sleep method is not suspending the execution even though i read that its a static method..
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

this one is pretty tricky I think. The issue is that the sleep of both threads is invoked at the end, because the thread run concurrently and the sleep is not in the loop.

Thread one will loop and print and then sleeps, but in the mean time thread 2 does the same. When you put a print statement after the sleep statement, you will see that the threads do actually sleep.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By printing timestamps it would be clear.

Try this code:


When you run this code o/p will be something like this:

Thread[joe,5,main] 0
Thread[joe,5,main] 1
Thread[joe,5,main] 2
Thread[joe,5,main] 3
Thread[joe,5,main] 4
Thread[jo,5,main] 0
Thread[jo,5,main] 1
Thread[jo,5,main] 2
Thread[jo,5,main] 3
Thread[jo,5,main] 4
Before sleep: Tue Jul 24 17:31:11 IST 2007 Thread Name: joe
Before sleep: Tue Jul 24 17:31:11 IST 2007 Thread Name: jo
After sleep: Tue Jul 24 17:31:21 IST 2007 Thread Name: joe
After sleep: Tue Jul 24 17:31:21 IST 2007 Thread Name: jo



And here we can see that both threads actually slpet for 10 seconds.

Murali...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Thread.sleep() method works for the currently executing thread...and I think the code is working as expected...
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. That code as written will write the message out 5 times, quick like a bunny rabbit, and then sleep for ten seconds....once. If you wanted (message and a ten second sleep) five times.... you will need curly braces around the block after the "for()".
 
saravana.T kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your explanations and Examples
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!