• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread without sleep?

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


When we use Thread, we should call Thread.sleep() so that other thread has a chance to execute. However, in my simple app above, I do not need to use Thread.sleep(...), but both thread still have a chance to execute. So, why is it ? Is it because of multicore in my CPU ? If so, we do not need to use Thread.sleep(...). Can anyone please help me ? Thanks a lot.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

CongSon Nguyen wrote:
When we use Thread, we should call Thread.sleep() so that other thread has a chance to execute.



Where did you hear this? ... because this is simply not true.


It may have been somewhat true, back with the early JVMs, when it was implemented on top of a user threading library (green threads). However, even with green threads, this was only true if you were completely compute intensive -- it did scheduling when you made library calls, like the system out IO calls.

Henry
 
CongSon Nguyen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't get it. Can you please explain more clearly ?

Java.sun.com said "Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system".
Even when we don't use Thread.sleep(...), processor time is still available to the other threads -> I do not really understand it.
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, what they probably mean is that you should use Thread.sleep() instead of some busy wait (looping in code that does nothing until a certain amount of time has elapsed).
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic