<div dir="ltr" style="text-align: justify;" trbidi="on">In this article you can learn the basic steps of creating a thread; this article provides two ways for creating your own thread in Java.</p>
<p><b>Multithreading </b>is a process of executing multiple threads simultaneously. A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). <b>Multithreaded </b>programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. <b>For example</b>, one thread is writing content on a file at the same time another thread is performing spelling check.</p>
<p> <b>Thread </b>is basically a lightweight <b>subprocess</b>, a smallest unit of processing. Multiprocessing and <b>multithreading</b>, both are used to achieve <b>multitasking</b>. But we use <b>multithreading </b>than <b>mulitprocessing</b> because threads share a common memory area. They don&#8217;t allocate separate memory area so save memory, and context-switching between the threads takes less time than processes.<br />
<b>Multithreading </b>is mostly used in games, animation etc.</p>
<p><b>Multitasking </b>is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways: </p>
<ul>
<li>Process-based Multitasking(<b>Multiprocessing</b>)</li>
<li>Thread-based Multitasking(<b>Multithreading</b>)</li>
</ul>
<div align='center' id="ads-id"></div>
<p>In Java, the<b> <a href="https://dineshonjava.com/2013/02/java-virtual-machine.html">Java Virtual Machine (JVM)</a></b> allows an application to have multiple threads of execution running concurrently. It allows a program to be more responsible to the user. When a program contains multiple threads then the CPU can switch between the two threads to execute them at the same time.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/JavaMu4.gif"/></div>
<p>In this diagram, two threads are being executed having more than one task. The task of each thread is switched to the task of another thread.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/thread-call-stack.jpg"/></div>
<p>
<b>1)Process-based Multitasking (Multiprocessing)</b></p>
<ul>
<li>Each process have its own address in memory i.e. each process allocates separate memory area.</li>
<li>Process is heavyweight.</li>
<li>Cost of communication between the process is high.</li>
<li>Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.</li>
</ul>
<p>
<b>2)Thread-based Multitasking (Multithreading)</b></p>
<ul>
<li>Threads share the same address space.</li>
<li>Thread is lightweight.</li>
<li>Cost of communication between the thread is low.</li>
<li>Note:At least one process is required for each thread.</li>
</ul>
<p><b>Advantages of multithreading over multiprocessing : </b></p>
<ul>
<li>Reduces the computation time.</li>
<li>Improves performance of an application.</li>
<li>Threads share the same address space so it saves the memory.</li>
<li>Context switching between threads is usually less expensive than between processes. </li>
<li>Cost of communication between threads is relatively low.</li>
</ul>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/nested-interface-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/life-cycle-of-thread/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/life-cycle-of-thread/">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: '485'});
});
</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…