• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Sleep, main thread

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is thread.sleep method causes main thread to sleep for 2 seconds in the above code
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. Your code fails to comple cause you do not catch InterruptedException
2. Did you try to test it yourself?
3. Did you read the javadoc web page

YES it does wait for 2 seconds.
Try these and if you have doubts your welcome
Thanks
Deepak
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the truth this code nor compels, the method sleep must place between try{} and catch{} for launching a verified exception. But the value that you is placed in args of this method says that thread that it executed this method at the very least goes to be that time without executing. If it will be interrupted before, an exception goes to happen. But nothing he says that the time after to pass that thread comes to enter in the execution state. It can go for the executable state or execution the agendador that goes to say this. "when she says yourself of threads, she does not have guarantees"
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Camilo,
Pardon me, I tried my best to understand but its very difficult to understand your accent.
The code does compile fine and sleep() call is placed in the try{} and we do catch InterruptedException in catch().

Thanks
Deepak
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Thread t = new Thread(r);
2)t.start();
3)t.sleep(2000);
4)System.out.println("Will it be printed immediately or after 2 secs");

Hi,
Now that you say the compilation is fine, the thread will not wait for 2secs cos,the line 3 in the code has to get executed before the line 4 and now that there are only two threads( main and t) as per the code, the Thread Scheduler will have to execute the statements from the main thread and therefore the 4th line is executed while the thread t is SLEEPING.!!!

Get back to me regarding this, for further discussions.....
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question was not related to whether it will compile or not.
My question was that is main thread's print method executes immediately or after 2 seconds. I have written the code and execute it. It prints after 2 seconds. but why? as sleep method is performed on thread t not main
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:

3. Did you read the javadoc web page

YES it does wait for 2 seconds.
Try these and if you have doubts your welcome
Thanks
Deepak



Yes I found sleep method is a static method which Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds. and currently executing thread is main thread m i right
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Now I understand your question
See as the javadoc says sleep() will stop the currently executing thread for specified number of milli seconds also note that sleep() is a static method, so even if you call it "t.sleep(100)" it will still stop the currently executing thead and not t .
Now the question is which is the currently executing thread, The answer is main() because you have placed sleep() in main() and hence main thread will stop for 2 seconds, If you place sleep() in run() as shown below

then whenever the run() is invoked via a call to start() it will stop the currently executing thread which now is the new thread T1.
Remember sleep() is a static method so this should make it clear that it will stop the currently executing thread and even if you call it using a refrence t.thread() it doesn not make a difference. Also always invoke a static method using a class name , i think this is what led to confusion.
Hope its clear now.
Thanks
Deepak
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic