<div dir="ltr" style="text-align: justify;" trbidi="on">There are some difference key notes i conclude after working on <i><b>wait and sleep</b></i>, first take a look on sample using<i><b> wait()</b></i> and <i><b>sleep()</b></i>:</p>
<p> <b>Example1: using <i>wait()</i> and <i>sleep()</i>:</b></p>
<pre class="highlight" name="code">synchronized(HandObject) {
 while(isHandFree() == false) {
 /* Hand is still busy on happy coding or something else, please wait */
 HandObject.wait();
 }
}

/* Get lock ^^, It is my turn, take a cup beer now */
while (beerIsAvailable() == false) {
 /* Beer is still coming, not available, Hand still hold glass to get beer,
 don't release hand to perform other task */
 Thread.sleep(5000);
}

/* Enjoy my beer now ^^ */
drinkBeers();

/* I have drink enough, now hand can continue with other task: continue coding */
setHandFreeState(true);
HandObject.notifyAll();
</pre>
<div align='center' id="ads-id"></div>
<p>Let clarity some key notes: </p>
<ol>
<li><b>Call on</b>:
<ul>
<li><i><b>wait():</b></i> Call on current thread that hold <i><b>HandObject </b></i>Object </li>
<li><b><i>sleep(): </i></b>Call on Thread execute task get beer (is class method so affect on current running thread)</li>
</ul>
</li>
<li><b>Synchronized</b>:
<ul>
<li><i><b>wait():</b></i> when synchronized multi thread access same <i><b>Object (HandObject) </b></i>(When need communication between more than one thread (thread execute coding, thread execute get beer) access on same object <i><b> HandObject </b></i>)</li>
<li><b><i>sleep():</i></b> when waiting condition to continue execute (Waiting beer available)</li>
</ul>
</li>
<li><b>Hold lock</b>:
<ul>
<li><i><b>wait():</b></i> release the lock for other object have chance to execute (<b><i>HandObject </i></b>is free, you can do other job)</li>
<li><i><b>sleep():</b></i> keep lock for at least t times (or until interrupt) (My job still not finish, I&#8217;m continue hold lock and waiting some condition to continue)</li>
</ul>
</li>
<li><b>Wake-up condition</b>:
<ul>
<li><i><b>wait(): </b></i>until call<i><b> notify(), notifyAll()</b></i> from object</li>
<li><i><b>sleep():</b></i> until at least time expire or call interrupt </li>
</ul>
</li>
</ol>
<pre class="highlight" name="code">synchronized(LOCK) {
 Thread.sleep(1000); // LOCK is held
}


synchronized(LOCK) {
 LOCK.wait(); // LOCK is not held
}
</pre>
<p>
It all eventually makes its way down to the OS’s scheduler, which hands out timeslices to processes and threads.</p>
<p> <b>sleep(n) says “I’m done with my timeslice, and please don’t give me another one for at least n milliseconds.</b>” The OS doesn’t even try to schedule the sleeping thread until requested time has passed.</p>
<p> yield() says “<b>I’m done with my timeslice, but I still have work to do.</b>” The OS is free to immediately give the thread another timeslice, or to give some other thread or process the CPU the yielding thread just gave up.</p>
<p> .wait() says <b>“I’m done with my timeslice. Don’t give me another timeslice until someone calls notify()</b>.” As with sleep(), the OS won’t even try to schedule your task unless someone calls notify() (or one of a few other wakeup scenarios occurs).</p>
<p> Threads also lose the remainder of their timeslice when they perform blocking IO and under a few other circumstances. If a thread works through the entire timeslice, the OS forcibly takes control roughly as if <i><b>yield() </b></i>had been called, so that other processes can run.</p>
<p> You rarely need <i><b>yield()</b></i>, but if you have a compute-heavy app with logical task boundaries, inserting a <b><i>yield()</i></b> might improve system responsiveness (at the expense of time — context switches, even just to the OS and back, aren’t free). Measure and test against goals you care about, as always.</p>
<p><b>Difference between yield and sleep in java</b><br />
Major difference between <i><b>yield </b></i>and <i><b>sleep </b></i>in Java is that <b><i>yield()</i></b> method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent. Yield method doesn’t guarantee that current thread will pause or stop but it guarantee that CPU will be relinquish by current Thread as a result of call to <b><i>Thread.yield()</i></b> method in java.</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/05/interrupting-java-threads.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/05/java-collection-framework.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/interrupting-java-threads/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/introduction-to-wsdl/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '470'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…