<div dir="ltr" style="text-align: justify;" trbidi="on">In Java, an object of the Thread class can represent a thread. Thread can be implemented through any one of two ways:</p>
<ol>
<li>Extending the <i><b>java.lang.Thread</b></i> Class</li>
<li>Implementing the <i><b>java.lang.Runnable</b></i> Interface</li>
</ol>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/thread3.gif"/></div>
<p><b>Extending the <i>java.lang.Thread</i> Class</b></p>
<p> For creating a thread a class have to extend the <i><b>Thread </b></i>Class. For creating a thread by this procedure you have to follow these steps:</p>
<ol>
<li>Extend the <i><b>java.lang.Thread</b></i> Class.</li>
<li>Override the <i><b>run( )</b></i> method in the subclass from the Thread class to define the code executed by the thread.</li>
<li>Create an instance of this subclass. This subclass may call a Thread class constructor by subclass constructor.</li>
<li>Invoke the <i><b>start( )</b></i> method on the instance of the class to make the thread eligible for running.</li>
</ol>
<p><b>By extend</b> the <i><b>Thread </b></i>class of <i><b>java.lang </b></i>package.<br />
<b>Syntax&#8230;&#8230;&#8230;..</b></p>
<pre class="highlight" name="code">class MyThread extends Thread 
{ 
-----------------; 
-----------------; 
} 
</pre>
<p><b>Override the <i>run( )</i> Method-</b>The <i>run( )</i> method has to be Overridden by writing codes required for the thread. The thread behaves as per this code segment.</p>
<pre class="highlight" name="code">public void run() 
{ 
----------------; 
----------------; 
} 
</pre>
<div align="center" id="ads-id"></div>
<p><b>Commonly used Constructors of Thread class:</b></p>
<ul>
<li>Thread()</li>
<li>Thread(String name)</li>
<li>Thread(Runnable r)</li>
<li>Thread(Runnable r,String name)</li>
</ul>
<p><b>Commonly used methods of Thread class:</b></p>
<ul>
<li><b>public void run():</b> is used to perform action for a thread.</li>
<li><b>public void start():</b> starts the execution of the thread.JVM calls the run() method on the thread.</li>
<li><b>public void sleep(long miliseconds):</b> Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.</li>
<li><b>public void join(): </b>waits for a thread to die.</li>
<li><b>public void join(long miliseconds): </b>waits for a thread to die for the specified miliseconds.</li>
<li><b>public int getPriority(): </b>returns the priority of the thread.</li>
<li><b>public int setPriority(int priority):</b> changes the priority of the thread.</li>
<li><b>public String getName(): </b>returns the name of the thread.</li>
<li><b>public void setName(String name):</b> changes the name of the thread.</li>
<li><b>public Thread currentThread(): </b>returns the reference of currently executing thread.</li>
<li><b>public int getId():</b> returns the id of the thread.</li>
<li><b>public Thread.State getState(): </b>returns the state of the thread.</li>
<li><b>public boolean isAlive():</b> tests if the thread is alive.</li>
<li><b>public void yield(): </b>causes the currently executing thread object to temporarily pause and allow other threads to execute.</li>
<li><b>public void suspend():</b> is used to suspend the thread(depricated).</li>
<li><b>public void resume():</b> is used to resume the suspended thread(depricated).</li>
<li><b>public void stop():</b> is used to stop the thread(depricated).</li>
<li><b>public boolean isDaemon():</b> tests if the thread is a daemon thread.</li>
<li><b>public void setDaemon(boolean b):</b> marks the thread as daemon or user thread.</li>
<li><b>public void interrupt(): </b>interrupts the thread.</li>
<li><b>public boolean isInterrupted():</b> tests if the thread has been interrupted.</li>
<li><b>public static boolean interrupted(): </b>tests if the current thread has been interrupted.</li>
</ul>
<p><b>Extending Thread class Example</b></p>
<p> This is a way to create a thread by a new class that extends <i><b>Thread </b></i>class and create an instance of that class. The extending class must override <i><b>run() </b></i>method which is the entry point of new thread.</p>
<pre class="highlight" name="code">class MyThread extends Thread 
{ 
 public void run() 
 { 
 System.out.println("Concurrent thread started running.."); 
 } 
} 
 
class MyThreadDemo 
{ 
 public static void main( String args[] ) 
 { 
 MyThread mt = new MyThread(); 
 mt.start(); 
 } 
} 
</pre>
<p>In this case also, as we must override the <i><b>run()</b></i> and then use the <b><i>start()</i></b> method to start and run the thread. Also, when you create <b>MyThread </b>class object, <i><b>Thread </b></i>class constructor will also be invoked, as it is the super class, hence <b>MyThread </b>class object acts as <b><i>Thread </i></b>class object.<br />
<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/threads.png"/></div>
<p><b>What if we call run() method directly without using start() method ?</b><br />
In above program if we directly call run() method, without using start() method,</p>
<pre class="highlight" name="code">public static void main( String args[] ) 
{ 
 MyThread mt = new MyThread(); 
 mt.run(); 
} 
</pre>
<p>Doing so, the thread won&#8217;t be allocated a new call stack, and it will start running in the current call stack, that is the call stack of the main thread. Hence <b>Multithreading </b>won&#8217;t be there.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/thread-without-new-call-stack.gif"/></div>
<p><b>Can we Start a thread twice ?</b><br />
No, a thread cannot be started twice. If you try to do so, <i><b>IllegalThreadStateException </b></i>will be thrown.</p>
<pre class="highlight" name="code">public static void main( String args[] ) 
{ 
 MyThread mt = new MyThread(); 
 mt.start(); 
 mt.start(); //Exception thrown 
} 
</pre>
<p>When a thread is in running state, and you try to start it again, or any method try to invoke that thread again using <i><b>start()</b></i> method, exception is thrown.</p>
<p><b>Implementing the <i>Runnable </i>Interface:</b><br />
The easiest way to create a thread is to create a class that implements the runnable interface. After implementing runnable interface , the class needs to implement the run() method, which is of form,</p>
<pre class="highlight" name="code">public void run() 
</pre>
<ul>
<li><b>run() </b>method introduces a concurrent thread into your program. This thread will end when run() returns.</li>
<li>You must specify the code for your thread inside <b>run()</b> method.</li>
<li><b>run() </b>method can call other methods, can use other classes and declare variables just like any other normal method.</li>
</ul>
<pre class="highlight" name="code">class MyThread implements Runnable 
{ 
 public void run() 
 { 
 System.out.println("concurrent thread started running.."); 
 } 
} 
 
class MyThreadDemo 
{ 
 public static void main( String args[] ) 
 { 
 MyThread mt = new MyThread(); 
 Thread t = new Thread(mt); 
 t.start(); 
 } 
} 
</pre>
<p>To call the <b>run()</b> method, <b>start()</b> method is used. On calling <b>start()</b>, a new stack is provided to the thread and <b>run()</b> method is called to introduce the new thread into the program.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/threads.png"/></div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/life-cycle-of-thread/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/thread-scheduling-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/life-cycle-of-thread/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/thread-scheduling-in-java/">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: '483'});
});
</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…