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

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago