<div dir="ltr" style="text-align: justify;" trbidi="on">The <i><b>Thread.sleep()</b></i> method effectively &#8220;pauses&#8221; the current thread for a given period of time. We used it in our very first threading example to make threads display a message periodically, sleeping between messages. From the outset, it&#8217;s important to be aware of the following:</p>
<ul>
<li>it is always the current thread that is put to sleep;</li>
<li>the thread might not sleep for the required time (or even at all);</li>
<li>the sleep duration will be subject to some system-specific granularity, typically 1ms;</li>
<li>while sleeping, the thread still owns synchronization locks it has acquired;</li>
<li>the sleep can be interrupted (sometimes useful for implementing a cancellation function);</li>
<li>calling sleep() with certain values can have some subtle, global effects on the OS (see below), and vice versa, other threads and processes running on the system can have subtle effects on the observed sleep duration.</li>
</ul>
<p><b>Syntax of sleep() method:</b><br />
The Thread class provides two methods for sleeping a thread:</p>
<ol>
<li><i><b>public static void sleep(long miliseconds)throws InterruptedException</b></i></li>
<li><i><b>public static void sleep(long miliseconds, int nanos)throws InterruptedException</b></i></li>
</ol>
<div align="center" id="ads-id"></div>
<pre class="highlight" name="code">class MultiThreadDemo extends Thread{ 
 public void run(){ 
 for(int i=1;i<;5;i++){ 
 try{ 
 Thread.sleep(500); 
 }catch(InterruptedException e){ 
 System.out.println(e); 
 } 
 System.out.println("Dinesh on Java Thread Application "+i); 
 } 
 } 
 public static void main(String args[]){ 
 MultiThreadDemo t1 = new MultiThreadDemo(); 
 MultiThreadDemo t2 = new MultiThreadDemo(); 
 
 t1.start(); 
 t2.start(); 
 } 
} 
</pre>
<p><b>Output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/sleep.png" /></div>
<p>
 As you know well that at a time only one thread is executed. If you sleep a thread for the specified time,the thread scheduler picks up another thread and so on. ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/thread-scheduling-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/java-thread-join-using-join-method/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/thread-scheduling-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-thread-join-using-join-method/">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: '481'});
});
</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…