<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tejal Gadad on Medium]]></title>
        <description><![CDATA[Stories by Tejal Gadad on Medium]]></description>
        <link>https://medium.com/@tejal.gadad20?source=rss-4b37a178d049------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*u57uabl8d2-a4s8m</url>
            <title>Stories by Tejal Gadad on Medium</title>
            <link>https://medium.com/@tejal.gadad20?source=rss-4b37a178d049------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 22:57:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tejal.gadad20/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Role of start() and run() in Multithreading in Java]]></title>
            <link>https://medium.com/@tejal.gadad20/role-of-start-and-run-in-multithreading-in-java-9f58755eeef1?source=rss-4b37a178d049------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f58755eeef1</guid>
            <category><![CDATA[start-method]]></category>
            <category><![CDATA[run-method]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[multithreading-in-java]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Tejal Gadad]]></dc:creator>
            <pubDate>Thu, 30 Dec 2021 13:57:43 GMT</pubDate>
            <atom:updated>2022-01-24T08:55:39.587Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>We often run various applications on our PC at the same time, open multiple classes in our IDE at the same time while working on a program. Have you ever wondered how can we open multiple tabs in our web browser at the same time as we all love to listen to music simultaneously while doing our homework?</em></blockquote><p>Its because of these concepts called -</p><ol><li>MultiProgramming</li><li>MultiTasking</li><li>MultiProcessing</li><li>MultiThreading</li></ol><p><strong><em>How does multithreading work?</em></strong></p><p>For a program to be executed, the Operating System has to load the program from secondary storage to the main memory. In a multithreaded application this one process will have multiple threads inside. It is important to note that only one thread is executed at a particular time and context-switching takes place between them. Threads are lightweight independent, so it doesn’t affect other threads whether or not an exception occurs during a single thread execution. If a process has multiple threads they will share similar properties such as the code, global and static variables, dynamic heap memory and process control block, the only difference is in stack space. Each thread will have its own stack space which holds the call stack, as the threads share the space of the process’s memory it is beneficial to have multiple threads of the process.</p><p><strong><em>Why do we need multithreading?</em></strong></p><p>Multithreading enables us to boost the responsiveness of a system, makes it easy to perform multiple operations at an identical time, since we perform many operations together, so it saves time. One can think of gaming applications and animation as examples of multithreading.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*SpjTqmUDvUEUntTiCfhT0Q.png" /><figcaption>Multithreading in java</figcaption></figure><p>Multithreading in java</p><p><strong><em>Now lets see how to implement multithreading in JAVA programs!</em></strong></p><p>In Java, the life cycle of a thread includes 5 states which are: New, Active ,Blocked / Waiting, Timed Waiting, Terminated</p><p>JAVA provides the Thread class to implement multithreading which is extended from the Object class and which implements Runnable interface.</p><p>We can create threads by using two mechanisms:</p><ol><li>Extending the Thread class</li><li>Implementing the Runnable Interface</li></ol><p>The Thread class contains multiple methods such as start(), run(), sleep(), currentThread(), join(), setPriority(), etc.</p><p>Do not worry Java runnable is nothing but an interface used to execute code on a concurrent thread. It can be implemented in any class that we want to run as a thread. The runnable interface has an undefined method run() which has void as its return type, and it takes in no arguments. The Runnable Interface is often used to override the run() method.</p><p>But what does this run() method do? And how can I create a thread using these two mechanisms?</p><p>Lets discuss start() and run() methods to understand these concepts better!</p><p><strong>start()</strong></p><p>The creation of a new method is done using the start() method. It starts the execution of a thread by creating two threads that are running concurrently.</p><p><strong>run()</strong></p><p>The start() method internally calls the run() method and the code inside the run() method is executed. If we directly call the run() method without the start() method then no thread is created and the run() method will be executed in the current thread itself.</p><p><strong>[1] Creation of thread by extending Thread class:</strong></p><blockquote><em>//Creating a new thread by extending Thread class and start()</em></blockquote><blockquote><em>class NewThread extends Thread {</em></blockquote><blockquote><em>public void run() {</em></blockquote><blockquote><em>System.out.println(“run() method is executed”);</em></blockquote><blockquote><em>System.out.println(“Name of the current thread created: “ + Thread.currentThread().getName());}}</em></blockquote><blockquote><em>public class MainClass{</em></blockquote><blockquote><em>public static void main(String [] arg) {</em></blockquote><blockquote><em>NewThread thread = new NewThread();</em></blockquote><blockquote><em>thread.start();}}</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*oxJ6UUAGlkB72swqdANUeQ.png" /></figure><p><strong>Output:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*CBhJQwTW2-DKcJ9DH6Y36w.png" /></figure><p>The start() method creates a thread with the default name Thread-0.</p><p><strong>[2] Creation of thread by implementing Runnable interface:</strong></p><blockquote>//Creating a new thread by implementing Runnable interface and start()</blockquote><blockquote>class RunnableThread implements Runnable {</blockquote><blockquote>public void run() {</blockquote><blockquote>System.out.println(“Name of the current thread created: “ + Thread.currentThread().getName());}}</blockquote><blockquote>public class MainThread{</blockquote><blockquote>public static void main(String [] arg) {</blockquote><blockquote>for (int i=0; i&lt;5; i++) {</blockquote><blockquote>Thread thread = new Thread(new RunnableThread());</blockquote><blockquote>thread.start(); }}}</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*m97qcfPZC3WOZtR-u7vtLw.png" /></figure><p><strong>Output:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*y2bhiakcR6JKo5eLTgoZKA.png" /></figure><p>The run() method is overridden in implementing the java.lang.Runnable interface. An object of the Thread class is instantiated and the start() method is called.</p><p><strong>[3] Call the run() method without start() method:</strong></p><blockquote>//Role of start() and run() in Multithreading — Blog</blockquote><blockquote>//Creating a new thread by extending Thread class and run() method without //start() method</blockquote><blockquote>class RunThread extends Thread {</blockquote><blockquote>public void run() {</blockquote><blockquote>System.out.println(“run() method is executed”);</blockquote><blockquote>System.out.println(“Name of the current thread created: “ + Thread.currentThread().getName());}}</blockquote><blockquote>public class MainRun{</blockquote><blockquote>public static void main(String [] arg) {</blockquote><blockquote>RunThread thread = new RunThread();</blockquote><blockquote>thread.run();}}</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*iKoL8JK-W_LBRJLfQJeH3Q.png" /></figure><p><strong>Output:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*Rb60JTZ_6x752A_EjalS0Q.png" /></figure><p>When start() method is not called and directly run() method is called then there is no creation of thread that takes place and the run() method is executed in the current thread i.e. main thread itself. This is called normal method call.</p><p><strong>[4] Calling start() method more than once:</strong></p><blockquote>//Role of start() and run() in Multithreading — Blog</blockquote><blockquote>//Creating thread by calling start thread multiple times</blockquote><blockquote>class StartThread extends Thread {</blockquote><blockquote>public void run() {</blockquote><blockquote>System.out.println(“run() method is executed”);</blockquote><blockquote>System.out.println(“Name of the current thread created: “ + Thread.currentThread().getName());}}</blockquote><blockquote>public class MainStart{</blockquote><blockquote>public static void main(String [] arg) {</blockquote><blockquote>StartThread thread = new StartThread();</blockquote><blockquote>thread.start();</blockquote><blockquote>thread.start();}}</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*c5sb7vR22slV0vKlvaxJZg.png" /></figure><p><strong>Output:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*mmWgOLXZhSrvq-OH_rYBSQ.png" /></figure><p><strong>Note:</strong> The start() method cannot be called more than one times otherwise it throws an IllegalThreadStateException</p><p>To create multiple threads we can make use of a for loop and mention the number of iterations it should perform depending on the number of threads that are to be created</p><p><strong>[5] Calling start() and run() methods together:</strong></p><blockquote>//Role of start() and run() in Multithreading — Blog</blockquote><blockquote>//Creating thread by calling start() and run() methods</blockquote><blockquote>class CombinedThread extends Thread {</blockquote><blockquote>public void run() {</blockquote><blockquote>System.out.println(“run() method is executed”);</blockquote><blockquote>System.out.println(“Name of the current thread created: “ + Thread.currentThread().getName());}}</blockquote><blockquote>public class MainCombined{</blockquote><blockquote>public static void main(String [] arg) {</blockquote><blockquote>CombinedThread thread = new CombinedThread();</blockquote><blockquote>thread.start();</blockquote><blockquote>thread.run();}}</blockquote><blockquote>class StartThread extends Thread {</blockquote><blockquote>public void run() {</blockquote><blockquote>System.out.println(“run() method is executed”);</blockquote><blockquote>System.out.println(“Name of the current thread created: “ + Thread.currentThread().getName());}}</blockquote><blockquote>public class MainStart{</blockquote><blockquote>public static void main(String [] arg) {</blockquote><blockquote>StartThread thread = new StartThread();</blockquote><blockquote>thread.start();</blockquote><blockquote>thread.start();}}</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*HCIBaMOM73f_Vs_Lpb6Zqw.png" /></figure><p><strong>Output:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*hJyrShH-2uDFemTJbnVW7g.png" /></figure><p>When the run() method is called the second time it executes in the main thread as a normal method call and not in the newly created Thread-0.</p><p><strong>CONCLUSION</strong></p><p>Let’s summarize what we have discussed above,</p><ul><li>A thread can be created by either extending the Thread class or implementing the Runnable interface. Since Java does not support multiple inheritance, when we implement multithreading in Java by extending the Thread class, our class cannot extend any other class, but if we implement the Runnable interface, our class can still extend other parent classes.</li><li>Thread class contains multiple methods that are not available in the Runnable interface.</li><li>The start() method is used to create a thread which internally calls the run() method and the code inside it is executed.</li><li>Calling the start() method more than one times throws an IllegalThreadStateException</li></ul><p>The efficiency of computer software depends on its ability to process the instructions at a faster pace. Multithreading operations help to meet these objectives. One plus point being that Java programming inherently provides multithread facilities within the coding and makes it easy to form and manage multiple threads in Java.</p><p><strong>Thank You for reading Our Blog…</strong></p><p><strong>Blog by — Parth Fulsoundar | Tejal Gadad | Shruti Ghulaxe | Asmita Gitte</strong></p><p>Vishwakarma Institue of Technology, Pune.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f58755eeef1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>