<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Threads · MangoDriod</title><link>https://md.eknath.dev/tags/threads/</link><description>Threads updates from MangoDriod</description><language>en-us</language><atom:link href="https://md.eknath.dev/tags/threads/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Wed, 25 Nov 2026 09:45:04 +0530</lastBuildDate><item><title>Introduction to Threads in Java</title><link>https://md.eknath.dev/posts/java/introduction-to-threads-in-java/</link><guid isPermaLink="true">https://md.eknath.dev/posts/java/introduction-to-threads-in-java/</guid><pubDate>Sat, 12 Feb 2022 12:26:00 +0530</pubDate><category>java</category><category>threads</category><category>concurrency</category><description>Threads are basically a way to run multiple tasks concurrently this can be helpful to speed up your program, usually, a Java program runs on a single thread by utilizing only one core of your processor here the other processors are just sitting idly by introducing thread java helps us to make use of these idle processors.
How to create a new thread process?
There are two ways of creating a new thread is by extending the Thread class and another way is by implementing Runnable interface either way the class must override the run method which will be called during the start method of the thread object.</description><content:encoded><![CDATA[<p>Threads are basically a way to run multiple tasks concurrently this can be helpful to speed up your program, usually, a Java program runs on a single thread by utilizing only one core of your processor here the other processors are just sitting idly by introducing thread java helps us to make use of these idle processors.</p>
<p><strong>How to create a new thread process?</strong></p>
<p>There are two ways of creating a new thread is by extending the Thread class and another way is by implementing Runnable interface either way the class must override the run method which will be called during the start method of the thread object.</p>
<p><em><strong>1. extending the Thread class</strong></em></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">extends</span> Thread {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><em><strong>2. Implementing Runnable Interface</strong></em></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">implements</span> Runnable {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>The ideal way would be by implementing the Runnable interface because this enables us to extend other required base classes if required, here the method run() will be called after the thread is started, here you may call the methods which you want to sublet or do any required process inside it, please note the run() returns nothing.</p>
<p><strong>Creating a thread Object.</strong></p>
<p>Creation of Thread object is same as creating any other object, <em>Thread &lt;name&gt; = new Thread(&lt;Object&gt;)</em> here a thread object will be created, now we can access the methods of the Thread class.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">extends</span> Thread {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
</span></span><span style="display:flex;"><span>    Main thread <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Main();
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="how-to-start-the-thread-">How to start the thread ?</h3>
<p>The thread can be started by calling the start method which is available in the thread class, for example <em>ThreadName.start()</em> here when the start method is called the process will start and eventually it will automatically call the run method and the thread will become active and start processing the methods inside the run method.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Main</span> <span style="color:#66d9ef">extends</span> Thread {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
</span></span><span style="display:flex;"><span>    Main thread <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Main();
</span></span><span style="display:flex;"><span>    thread.<span style="color:#a6e22e">start</span>();
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is outside of the thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;This code is running in a thread&#34;</span>);
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h4 id="lifecycle-of-thread">LifeCycle of Thread</h4>
<p>A thread exists only in any one of these states at a time, lets look at them one by one.</p>
<ul>
<li><strong>New</strong> as the name specifies this is a new state where the thread object is created, but none of the code inside it has started to execute yet.</li>
<li><strong>Runnable</strong> At this point the code inside this block is ready to run at any instant, once the thread scheduler flags green the code inside it will start to run one by one until there is any interruption.</li>
<li>Blocked</li>
<li>Waiting</li>
<li>Timed Waiting</li>
<li>Terminated</li>
</ul>
<hr>
<p><em>Note: This article was originally published on Blogger on February 12, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item></channel></rss>