<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ehis Edemakhiota</title>
    <description>The latest articles on DEV Community by Ehis Edemakhiota (@ehizman).</description>
    <link>https://dev.to/ehizman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F734392%2F647d8cec-51a0-4560-ae0d-d4bf02bc43e6.jpeg</url>
      <title>DEV Community: Ehis Edemakhiota</title>
      <link>https://dev.to/ehizman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ehizman"/>
    <language>en</language>
    <item>
      <title>Java Study Group: Motivation, Roadmap and Goals</title>
      <dc:creator>Ehis Edemakhiota</dc:creator>
      <pubDate>Wed, 18 Mar 2026 12:22:08 +0000</pubDate>
      <link>https://dev.to/ehizman/java-study-group-motivation-roadmap-and-goals-2k77</link>
      <guid>https://dev.to/ehizman/java-study-group-motivation-roadmap-and-goals-2k77</guid>
      <description>&lt;p&gt;Hi there!&lt;/p&gt;

&lt;p&gt;My name is &lt;a href="https://linkedin.com/in/ehisedemakhiota" rel="noopener noreferrer"&gt;Ehis Edemakhiota&lt;/a&gt; and I’m excited to see that you’ve come here. I am a Software Engineer and Java is my first language- I have been writing Java for about 5 years. I enjoy writing and thinking in Java because it is structured, dependable and if understood well, can be used to build software that is scalable and reliable. I want to be like Java :).&lt;/p&gt;

&lt;p&gt;A famous quote says: "&lt;em&gt;if you want to go quickly then go alone, but if you want to go far then go with people&lt;/em&gt;" . I want to go far and I want to do so with you :). This is my motivation for starting this study group.&lt;/p&gt;

&lt;p&gt;The goal of the study group is for beginner to expert level Java programmers to in 8-12 weeks be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;build production-ready Spring Boot applications, &lt;/li&gt;
&lt;li&gt;be prepared for Java/ Spring Boot technical interviews, &lt;/li&gt;
&lt;li&gt;contribute to real-world backend projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The topics of interest are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to Java and Java control structures&lt;/li&gt;
&lt;li&gt;Introduction to OOP and SOLID principles&lt;/li&gt;
&lt;li&gt;Design Patterns&lt;/li&gt;
&lt;li&gt;Java Arrays and Data structures&lt;/li&gt;
&lt;li&gt;Functional programming in Java&lt;/li&gt;
&lt;li&gt;Java concurrency&lt;/li&gt;
&lt;li&gt;JVM Internals&lt;/li&gt;
&lt;li&gt;Introduction to Spring Boot&lt;/li&gt;
&lt;li&gt;Spring Boot persistence best practices&lt;/li&gt;
&lt;li&gt;Spring Security&lt;/li&gt;
&lt;li&gt;Logging and Observability in Spring Boot&lt;/li&gt;
&lt;li&gt;Project Loom- Java 21 Virtual Threads&lt;/li&gt;
&lt;li&gt;Problem solving with LeetCode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of the study materials include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Boot in Action&lt;/li&gt;
&lt;li&gt;Spring Boot Up and Running&lt;/li&gt;
&lt;li&gt;Spring Persistence Best Practices.&lt;/li&gt;
&lt;li&gt;Relevant Medium and Baeldung.com articles&lt;/li&gt;
&lt;li&gt;Articles from the Offical Spring documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Java &amp;amp; Spring Boot — 12-Week Study Group Roadmap
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Format:&lt;/strong&gt; 4 phases · 12 weekly sessions · Mixed levels (Beginner → Expert)&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Goals:&lt;/strong&gt; Build production-ready Spring Boot applications · Ace Java/Spring Boot interviews · Contribute to real-world backend projects&lt;/p&gt;




&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Phase&lt;/th&gt;
&lt;th&gt;Title&lt;/th&gt;
&lt;th&gt;Weeks&lt;/th&gt;
&lt;th&gt;Focus&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Java Foundations&lt;/td&gt;
&lt;td&gt;1–3&lt;/td&gt;
&lt;td&gt;Core Java syntax, OOP, Design Patterns + LeetCode&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Advanced Java&lt;/td&gt;
&lt;td&gt;4–6&lt;/td&gt;
&lt;td&gt;Data Structures, Functional Programming, Concurrency&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;JVM &amp;amp; Spring Boot&lt;/td&gt;
&lt;td&gt;7–9&lt;/td&gt;
&lt;td&gt;JVM Internals, Spring Boot Core, Persistence&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Production-Ready&lt;/td&gt;
&lt;td&gt;10–12&lt;/td&gt;
&lt;td&gt;Security, Observability, Project Loom, Capstone&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Study Materials
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Spring Boot in Action&lt;/em&gt; — Craig Walls&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Spring Boot Up and Running&lt;/em&gt; — Mark Heckler&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Spring Persistence Best Practices&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Relevant articles from &lt;a href="https://www.baeldung.com" rel="noopener noreferrer"&gt;Baeldung.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Relevant articles from &lt;a href="https://www.medium.com" rel="noopener noreferrer"&gt;Medium.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.spring.io" rel="noopener noreferrer"&gt;Official Spring Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Phase 1 — Java Foundations (Weeks 1–3)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;On-ramp for beginners while giving intermediates a chance to solidify fundamentals. LeetCode problems are embedded in each session to immediately apply concepts.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Week 1 — Introduction to Java
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Setup, syntax, control flow &amp;amp; LeetCode warm-up&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Write basic Java programs, understand primitive types, operators, and control-flow structures. Begin applying these immediately through easy LeetCode problems.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;JDK / JRE setup&lt;/li&gt;
&lt;li&gt;Primitive types&lt;/li&gt;
&lt;li&gt;Operators&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if&lt;/code&gt; / &lt;code&gt;else&lt;/code&gt; / &lt;code&gt;switch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; / &lt;code&gt;while&lt;/code&gt; / &lt;code&gt;do-while&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Methods &amp;amp; scope&lt;/li&gt;
&lt;li&gt;String basics&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Java Basics series&lt;/li&gt;
&lt;li&gt;Official Java SE documentation&lt;/li&gt;
&lt;li&gt;Medium: Java 101 for beginners&lt;/li&gt;
&lt;li&gt;LeetCode: Explore — Java card&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  LeetCode Practice
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Focus:&lt;/strong&gt; Basic loops, conditionals &amp;amp; arithmetic. Solve in Java; submit and read other Java solutions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. Two Sum&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9. Palindrome Number&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;13. Roman to Integer&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;412. Fizz Buzz&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1342. Number of Steps to Zero&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Week 2 — OOP &amp;amp; SOLID Principles
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Classes, inheritance, interfaces &amp;amp; modelling kata&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Design Java classes using core OOP concepts, apply SOLID principles, and model solutions to LeetCode problems as well-structured classes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Classes &amp;amp; Objects&lt;/li&gt;
&lt;li&gt;Constructors&lt;/li&gt;
&lt;li&gt;Inheritance &amp;amp; &lt;code&gt;super&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;li&gt;Interfaces &amp;amp; abstraction&lt;/li&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;SOLID principles (S, O, L, I, D)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Java OOP guide&lt;/li&gt;
&lt;li&gt;Medium: SOLID principles in Java&lt;/li&gt;
&lt;li&gt;Official Java Docs: OOP trail&lt;/li&gt;
&lt;li&gt;LeetCode: OOP-friendly problems list&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  LeetCode Practice
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Focus:&lt;/strong&gt; Model your solution as a class first, then implement. Practice naming and responsibility separation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;155. Min Stack&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;146. LRU Cache&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;706. Design HashMap&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;232. Implement Queue using Stacks&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;303. Range Sum Query — Immutable&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Week 3 — Design Patterns
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; GoF creational, structural, behavioural &amp;amp; pattern-recognition kata&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Recognise and apply common GoF design patterns; spot patterns inside LeetCode problem structures and justify your pattern choice.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Singleton&lt;/li&gt;
&lt;li&gt;Factory / Abstract Factory&lt;/li&gt;
&lt;li&gt;Builder&lt;/li&gt;
&lt;li&gt;Observer&lt;/li&gt;
&lt;li&gt;Strategy&lt;/li&gt;
&lt;li&gt;Decorator&lt;/li&gt;
&lt;li&gt;Facade&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Design Patterns in Java&lt;/li&gt;
&lt;li&gt;Refactoring.guru pattern catalogue&lt;/li&gt;
&lt;li&gt;Medium: GoF patterns explained&lt;/li&gt;
&lt;li&gt;LeetCode: Patterns in coding problems (Medium article)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  LeetCode Practice
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Focus:&lt;/strong&gt; Identify which GoF pattern maps to the problem structure &lt;em&gt;before&lt;/em&gt; coding. Discuss your choice with the group.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;Difficulty&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;284. Peeking Iterator&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;341. Flatten Nested List Iterator&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;460. LFU Cache&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;716. Max Stack&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1603. Design Parking System&lt;/td&gt;
&lt;td&gt;Easy&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Phase 2 — Advanced Java (Weeks 4–6)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Raises the ceiling with Collections, the Streams/Lambda API, and Concurrency — high-frequency interview topics and prerequisites for understanding Spring Boot internals.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Week 4 — Arrays &amp;amp; Data Structures
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Collections framework &amp;amp; Big-O&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Select the right Java data structure for a problem and understand time &amp;amp; space complexity trade-offs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Arrays &amp;amp; multi-dimensional arrays&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ArrayList&lt;/code&gt; / &lt;code&gt;LinkedList&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HashMap&lt;/code&gt; / &lt;code&gt;TreeMap&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HashSet&lt;/code&gt; / &lt;code&gt;TreeSet&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Queue&lt;/code&gt; / &lt;code&gt;Deque&lt;/code&gt; / &lt;code&gt;Stack&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Big-O analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Java Collections guide&lt;/li&gt;
&lt;li&gt;Official Java Docs: Collections framework&lt;/li&gt;
&lt;li&gt;Medium: Data structures in Java&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Week 5 — Functional Programming in Java
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Streams, lambdas &amp;amp; Optional&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Write idiomatic functional-style Java using the Streams API, lambdas, and method references.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Lambda expressions&lt;/li&gt;
&lt;li&gt;Functional interfaces&lt;/li&gt;
&lt;li&gt;Stream API (&lt;code&gt;map&lt;/code&gt; / &lt;code&gt;filter&lt;/code&gt; / &lt;code&gt;reduce&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Collectors&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Optional&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Method references&lt;/li&gt;
&lt;li&gt;Parallel streams&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Java 8 Streams series&lt;/li&gt;
&lt;li&gt;Official Java Docs: Stream API&lt;/li&gt;
&lt;li&gt;Baeldung: Guide to Optional&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Week 6 — Java Concurrency
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Advanced&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Threads, locks &amp;amp; CompletableFuture&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Write thread-safe concurrent Java programs and use modern concurrency utilities effectively.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Thread lifecycle&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;synchronized&lt;/code&gt; &amp;amp; &lt;code&gt;volatile&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ExecutorService&lt;/code&gt; / thread pools&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Callable&lt;/code&gt; &amp;amp; &lt;code&gt;Future&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CompletableFuture&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ReentrantLock&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;java.util.concurrent&lt;/code&gt; package&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Java Concurrency series&lt;/li&gt;
&lt;li&gt;Official Java Docs: Concurrency utilities&lt;/li&gt;
&lt;li&gt;Medium: CompletableFuture deep dive&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Phase 3 — JVM &amp;amp; Spring Boot (Weeks 7–9)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Opens with JVM internals so developers understand &lt;em&gt;why&lt;/em&gt; their Spring apps behave the way they do, then moves into Spring Boot core and persistence.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Week 7 — JVM Internals
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Advanced&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Memory model, GC &amp;amp; class loading&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Understand JVM architecture in depth to profile, tune, and diagnose Java applications in production.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;ClassLoader subsystem&lt;/li&gt;
&lt;li&gt;Heap / Stack / Metaspace&lt;/li&gt;
&lt;li&gt;GC algorithms (G1, ZGC, Shenandoah)&lt;/li&gt;
&lt;li&gt;JIT compilation&lt;/li&gt;
&lt;li&gt;JVM tuning flags&lt;/li&gt;
&lt;li&gt;JFR / VisualVM profiling&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: JVM Internals series&lt;/li&gt;
&lt;li&gt;Medium: Deep dive into the JVM&lt;/li&gt;
&lt;li&gt;Official JVM specification&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Week 8 — Introduction to Spring Boot
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Beginner&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Auto-config, DI &amp;amp; REST APIs&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Bootstrap a Spring Boot project and build a functional REST API, understanding auto-configuration and dependency injection.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Spring IoC container&lt;/li&gt;
&lt;li&gt;Dependency injection&lt;/li&gt;
&lt;li&gt;Auto-configuration&lt;/li&gt;
&lt;li&gt;Spring Boot starters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@RestController&lt;/code&gt; / &lt;code&gt;@Service&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;application.yml&lt;/code&gt; &amp;amp; profiles&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Spring Boot in Action&lt;/em&gt; — Craig Walls&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Spring Boot Up and Running&lt;/em&gt; — Mark Heckler&lt;/li&gt;
&lt;li&gt;Official spring.io documentation&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Week 9 — Spring Persistence
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Spring Data JPA, transactions &amp;amp; optimisation&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Implement a robust, optimised persistence layer with Spring Data JPA and handle transactions correctly.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Spring Data JPA repositories&lt;/li&gt;
&lt;li&gt;Entity mapping &amp;amp; relationships&lt;/li&gt;
&lt;li&gt;JPQL &amp;amp; native queries&lt;/li&gt;
&lt;li&gt;N+1 problem &amp;amp; fetch strategies&lt;/li&gt;
&lt;li&gt;Transaction management&lt;/li&gt;
&lt;li&gt;Flyway / Liquibase migrations&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Spring Persistence Best Practices&lt;/em&gt; (book)&lt;/li&gt;
&lt;li&gt;Baeldung: Spring Data JPA series&lt;/li&gt;
&lt;li&gt;Official Spring Data JPA docs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Phase 4 — Production-Ready (Weeks 10–12)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Covers the operational concerns real teams care about: security, observability, and modern concurrency with Project Loom.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Week 10 — Spring Security
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Authentication, JWT &amp;amp; OAuth2&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Secure a Spring Boot application using JWT, OAuth2, and method-level authorisation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Security filter chain&lt;/li&gt;
&lt;li&gt;&lt;code&gt;UserDetailsService&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;JWT authentication&lt;/li&gt;
&lt;li&gt;OAuth2 / OpenID Connect&lt;/li&gt;
&lt;li&gt;Role-based access control&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@PreAuthorize&lt;/code&gt; / &lt;code&gt;@Secured&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Spring Boot Up and Running&lt;/em&gt; — Ch. Security&lt;/li&gt;
&lt;li&gt;Baeldung: Spring Security series&lt;/li&gt;
&lt;li&gt;Official Spring Security reference docs&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Week 11 — Logging &amp;amp; Observability
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Intermediate&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Structured logs, metrics &amp;amp; tracing&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Instrument a Spring Boot app for full production observability: structured logs, metrics, and distributed traces.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;SLF4J / Logback configuration&lt;/li&gt;
&lt;li&gt;Structured / JSON logging&lt;/li&gt;
&lt;li&gt;Spring Boot Actuator&lt;/li&gt;
&lt;li&gt;Micrometer metrics&lt;/li&gt;
&lt;li&gt;OpenTelemetry tracing&lt;/li&gt;
&lt;li&gt;Grafana / Prometheus integration&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Baeldung: Spring Boot logging guide&lt;/li&gt;
&lt;li&gt;Official Spring Actuator docs&lt;/li&gt;
&lt;li&gt;Medium: Observability in Spring Boot&lt;/li&gt;
&lt;li&gt;OpenTelemetry Java docs&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Week 12 — Project Loom &amp;amp; Capstone
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Level:&lt;/strong&gt; Advanced&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Subtitle:&lt;/strong&gt; Virtual threads + final project&lt;/p&gt;

&lt;h4&gt;
  
  
  Learning Goal
&lt;/h4&gt;

&lt;p&gt;Understand Project Loom, migrate to virtual threads, and deliver a capstone production-ready Spring Boot project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Virtual threads (JEP 444)&lt;/li&gt;
&lt;li&gt;Structured concurrency (JEP 453)&lt;/li&gt;
&lt;li&gt;Thread-per-request model&lt;/li&gt;
&lt;li&gt;Performance benchmarking&lt;/li&gt;
&lt;li&gt;Loom + Spring Boot integration&lt;/li&gt;
&lt;li&gt;Capstone project presentation&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Study Materials
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;JEP 444: Virtual Threads specification&lt;/li&gt;
&lt;li&gt;Baeldung: Project Loom guide&lt;/li&gt;
&lt;li&gt;Official Spring Docs: Virtual threads&lt;/li&gt;
&lt;li&gt;Medium: Spring Boot + Project Loom&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Format of each session
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;90–120 minutes per session — 20 min concept review, live coding exercise, then group discussion.&lt;/li&gt;
&lt;li&gt;Beginners with intermediates during coding exercises. Experts take ownership of advanced weeks (6, 7, 12) as lead facilitators.&lt;/li&gt;
&lt;li&gt;Where applicable, the last 20–30 minutes of each Phase 1 session for a live group LeetCode walk-through where one member screen-shares and others review the solution.&lt;/li&gt;
&lt;li&gt;Where applicable, before live coding include a Design Patterns Kata:  &lt;em&gt;"Which GoF pattern does this problem smell like, and why?"&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Capstone Project:&lt;/strong&gt; Members of the study group will be paired. Each pair will work together to ship a small but complete production-grade Spring Boot application demonstrating persistence, security, and observability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this feels like you and you can commit for the duration of the study group, then I invite you to &lt;a href="https://forms.gle/MDFfNtFX4bcAcRVd9" rel="noopener noreferrer"&gt;join me on this journey&lt;/a&gt;. Let's go far and grow deep together. &lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Solving The Add Two Numbers Leetcode Question</title>
      <dc:creator>Ehis Edemakhiota</dc:creator>
      <pubDate>Sat, 24 Jan 2026 23:09:50 +0000</pubDate>
      <link>https://dev.to/ehizman/solving-the-add-two-numbers-leetcode-question-1a5o</link>
      <guid>https://dev.to/ehizman/solving-the-add-two-numbers-leetcode-question-1a5o</guid>
      <description>&lt;p&gt;&lt;a href="https://leetcode.com/problems/add-two-numbers/description/" rel="noopener noreferrer"&gt;Question&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You are given the starting nodes of two LinkedLists. Each LinkedList represents a reversed integer value: that is the value &lt;code&gt;768&lt;/code&gt; is represent in linked list form as &lt;code&gt;8-&amp;gt;6-&amp;gt;7&lt;/code&gt;. The task is to add the numbers in the linked list and then return the head of a new linked list representing the sum with the numbers reversed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clarifying Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Are the values of the ListNodes between 0 and 9?&lt;/em&gt;&lt;/strong&gt; This question is important because we want to keep track of carry values after addition.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;What are the maximum values represented by a LinkedList?&lt;/em&gt;&lt;/strong&gt; This question is important because we want to it would determined the approach to take. If the maximum values represented by a linkedlist can be the &lt;code&gt;INTEGER.MAX_VALUE&lt;/code&gt; then this means that we cannot "integerify" both linked lists and then convert the result to a list and then return the head.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Do both input lists have the same number of nodes?&lt;/em&gt;&lt;/strong&gt; This question would determine the constraints in your solution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Approach 1: Brute Force
&lt;/h3&gt;

&lt;p&gt;The brute force approach to solving this is "integerify" both input lists, add the resulting integers and then finally convert the sum to a list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l1Str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;convertToInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l2Str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;convertToInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l1Str&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;l2Str&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;convertToList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;convertToList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;convertToInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;sb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StringBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The time complexity for this solution is &lt;code&gt;O(max(N,M))&lt;/code&gt; and space complexity is &lt;code&gt;O(n)&lt;/code&gt;. However this solution fails for the edge case &lt;code&gt;9999999991&lt;/code&gt; and &lt;code&gt;9&lt;/code&gt; because &lt;code&gt;9999999991&lt;/code&gt; overflows the java integer data type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach 2:
&lt;/h2&gt;

&lt;p&gt;My second approach was to traverse both lists and add the corresponding digits individually. This meant that I needed to track the carry (most significant digit after every addition), while I create a new list node with the least significant digit as the &lt;code&gt;val&lt;/code&gt;. I created two &lt;code&gt;ListNode&lt;/code&gt; variables one to keep track of the result and the other to keep track of the current additions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;dummy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;ListNode&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dummy&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l1Val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;l2Val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;val&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l1Val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;l2Val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;carry&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListNode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;carry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
                &lt;span class="n"&gt;l1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;l2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dummy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time Complexity: O(max(n,m))&lt;/p&gt;

&lt;p&gt;Space Complexity: O(n)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxcik0r17o65vbi4ubei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxcik0r17o65vbi4ubei.png" alt="Screenshot from Leetcode" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>linkedlists</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Java Thread Life Cycles</title>
      <dc:creator>Ehis Edemakhiota</dc:creator>
      <pubDate>Sat, 05 Mar 2022 12:11:36 +0000</pubDate>
      <link>https://dev.to/ehizman/java-thread-life-cycles-20p9</link>
      <guid>https://dev.to/ehizman/java-thread-life-cycles-20p9</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;A thread is the smallest unit of processing. A thread can be in different states in a cycle leading from its creation to its termination.&lt;br&gt;
This article explores the different states in the Java thread life cycle. This is the second article of a concurrency series. The first article introduced the concepts of Multi-threading, Concurrency, Asynchronous Programming and Parallel Programming. You might want to take a look &lt;a href="https://dev.to/ehizman/distinguishing-between-concurrency-parallelism-asynchronous-programming-and-multi-threading-3d4i"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Definition of Terms
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Process&lt;/strong&gt;: A process is an instance of a program that is currently being executed by the processor. Different instances of the same program can run on a computer as different processes. This means that when I run MS Word on my computer, for instance, it runs as a process. I can run different instances of MS Word on my computer. For each new instance of MS Word, the processor spins up a new process. Each process has its own unique stack, memory and data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread&lt;/strong&gt;: A thread is a subset of a process. A process starts with one thread called the main thread. The main thread can branch into other threads. A process that contains only the main thread is called a &lt;strong&gt;single-threaded process&lt;/strong&gt;. A process that contains more than one thread is called a &lt;strong&gt;multi-threaded process&lt;/strong&gt;. All threads within a process all share the same code-segment, data-segment, registers, stack and program counter.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Lifecycle of threads in Java
&lt;/h3&gt;

&lt;p&gt;Java supports multithreading. A Java program can be designed in such a way that its different parts run on different threads. The execution of a Java program begins from the &lt;code&gt;main&lt;/code&gt; method. The &lt;code&gt;main&lt;/code&gt; method is executed on a special thread called the main thread, which is created by the &lt;a href="https://en.wikipedia.org/wiki/Java_virtual_machine" rel="noopener noreferrer"&gt;Java Virtual Machine&lt;/a&gt;(JVM). Other threads are created as off-shoots of the main thread.&lt;/p&gt;

&lt;p&gt;In Java, threads belong to the class- &lt;code&gt;java.lang.Thread&lt;/code&gt;. You can create a Java thread by implementing the &lt;code&gt;Runnable&lt;/code&gt; interface as shown in the following block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
 * CREATING A JAVA THREAD BY IMPLEMENTING THE RUNNABLE INTERFACE
*/&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;//the run method contains code that the thread will execute&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A simple thread has been created by implementing the Runnable interface"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also create a Java thread by extending the &lt;code&gt;Thread&lt;/code&gt; class as shown in the following block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.ExecutorService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.Executors&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/*
* CREATING A JAVA THREAD BY EXTENDING THE THREAD CLASS
* */&lt;/span&gt;


&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleThread&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// the run method contains code that the thread will execute&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A simple thread has been created by extending the Thread class"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To execute both threads defined above, you create an &lt;a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html" rel="noopener noreferrer"&gt;ExecutorService&lt;/a&gt;. The &lt;code&gt;ExecutorService&lt;/code&gt; provides a thread pool that allows you to execute threads using the &lt;code&gt;execute&lt;/code&gt; method as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;//creating an executor service that executes threads&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Executor&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SimpleThread&lt;/span&gt; &lt;span class="n"&gt;simpleThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleThread&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt; &lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// create the thread pool&lt;/span&gt;
        &lt;span class="nc"&gt;ExecutorService&lt;/span&gt; &lt;span class="n"&gt;executorService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Executors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newCachedThreadPool&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;executorService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;simpleThread&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;executorService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//the shutdown method notifies the executor service to stop accepting new tasks&lt;/span&gt;
        &lt;span class="c1"&gt;//but the executor service still continues to run tasks that have already been accepted&lt;/span&gt;
        &lt;span class="n"&gt;executorService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;shutdown&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;A simple thread has be created by extending the Thread class
A simple thread has been created by implementing the Runnable interface

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also execute threads using the &lt;code&gt;Thread&lt;/code&gt; class by invoking the &lt;code&gt;start&lt;/code&gt; method. &lt;strong&gt;To execute a thread created by extending the &lt;code&gt;Thread&lt;/code&gt; class&lt;/strong&gt;, you have to invoke the &lt;code&gt;start&lt;/code&gt; method on the thread object as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Take note that you have to first create a class that extends java’s &lt;code&gt;Thread&lt;/code&gt; class. (Using the &lt;code&gt;SimpleThread&lt;/code&gt; class created above)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SimpleThread&lt;/span&gt; &lt;span class="n"&gt;simpleThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleThread&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;simpleThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;A simple thread has be created by extending the Thread class

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To execute a thread created by implementing the Runnable interface&lt;/strong&gt;, you have to pass in a &lt;code&gt;Runnable&lt;/code&gt; as an argument to the &lt;code&gt;Thread&lt;/code&gt; constructor. This is demonstrated in the code block below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Take note that you have to first create a class that implements java’s &lt;code&gt;Runnable&lt;/code&gt; class before you can do this.&lt;br&gt;
(Using the &lt;code&gt;SimpleThreadTwo&lt;/code&gt; class created above.)&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt; &lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;//passing the Runnable object as an argument into the Thread class&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;A simple thread has been created by implementing the Runnable interface

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is important to highlight that creating a thread by implementing the &lt;code&gt;Runnable&lt;/code&gt; interface is more encouraged than creating threads using the &lt;code&gt;Thread&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;java.lang.Thread&lt;/code&gt; class has a static enum property- &lt;code&gt;State&lt;/code&gt;. The &lt;code&gt;State&lt;/code&gt; property defines the potential state of a Java thread. During its life cycle, a java thread can belong to any of the following states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NEW&lt;/li&gt;
&lt;li&gt;RUNNABLE&lt;/li&gt;
&lt;li&gt;WAITING&lt;/li&gt;
&lt;li&gt;TIMED-WAITING&lt;/li&gt;
&lt;li&gt;BLOCKED&lt;/li&gt;
&lt;li&gt;TERMINATED&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following is a diagrammatic representation of the java thread lifecycle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2qfh394n2ljj93igb5s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2qfh394n2ljj93igb5s.jpg" alt="Java Thread Life Cycle" width="800" height="654"&gt;&lt;/a&gt;&lt;br&gt;
Source: &lt;a href="https://www.baeldung.com/" rel="noopener noreferrer"&gt;baeldung.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following section describes these thread states in more detail:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NEW&lt;/strong&gt;: When a thread is created, it is in the &lt;strong&gt;NEW&lt;/strong&gt; state. A thread remains in the new state until its &lt;code&gt;start&lt;/code&gt; method is called.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt; &lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="c1"&gt;//passing the Runnable object as an argument into the Thread constructor&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current state of thread is %s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getState&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;The current state of thread is NEW

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RUNNABLE&lt;/strong&gt;: A thread is in the &lt;strong&gt;RUNNABLE&lt;/strong&gt; state when the processor executes its &lt;code&gt;start&lt;/code&gt; method. Threads in the &lt;strong&gt;RUNNABLE&lt;/strong&gt; state are either &lt;strong&gt;READY&lt;/strong&gt; to run or are already &lt;strong&gt;RUNNING&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a thread transitions to the &lt;strong&gt;RUNNABLE&lt;/strong&gt; state, the thread starts first in the &lt;strong&gt;READY&lt;/strong&gt; state. At the &lt;strong&gt;READY&lt;/strong&gt; state, the thread has not been assigned processor time also called a &lt;strong&gt;quantum or a time slice&lt;/strong&gt;. When a thread is assigned a time slice, &lt;strong&gt;the thread is said to be dispatched&lt;/strong&gt;. A newly dispatched thread joins a queue of other dispatched threads that are also waiting to run. The operating system uses a &lt;a href="https://www.javatpoint.com/thread-scheduler-in-java" rel="noopener noreferrer"&gt;Thread Scheduler&lt;/a&gt; to determine which thread to run.&lt;/p&gt;

&lt;p&gt;When a thread starts to run, it transitions into its &lt;strong&gt;RUNNING&lt;/strong&gt; state. In the &lt;strong&gt;RUNNING&lt;/strong&gt; state, the task defined within the &lt;code&gt;run&lt;/code&gt; method of the threads is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt; &lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="c1"&gt;//passing the Runnable object as an argument into the Thread constructor&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;simpleThreadTwo&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="c1"&gt;// the start method executes the code defined in the run method of the thread&lt;/span&gt;
        &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%nThe current state of thread is %s%n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getState&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WAITING&lt;/strong&gt;: A thread enters the &lt;strong&gt;WAITING&lt;/strong&gt; state while it waits for another thread to perform a task. A running thread can be placed on wait when one of the following methods is invoked:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;wait&lt;/code&gt;: The wait method causes an object to give up its timeslice and to release the monitor lock on a synchronized object. A monitor lock is a contract that gives a thread sole access to an object. The wait method causes the object to give up processor time and to transition from the &lt;strong&gt;RUNNING&lt;/strong&gt; state to the &lt;strong&gt;WAITING&lt;/strong&gt; state. A thread in waiting can transition back into execution when the thread that currently has the monitor lock calls the &lt;code&gt;notify&lt;/code&gt; or &lt;code&gt;notifyAll&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LockSupport.park&lt;/code&gt;: The &lt;code&gt;LockSupport.park&lt;/code&gt; method disables a thread unless a permit is available. A permit is a permission for a thread to continue execution. When the &lt;code&gt;park&lt;/code&gt; method is called on the current thread, the thread remains in the &lt;strong&gt;WAITING&lt;/strong&gt; state. When some other thread invokes the &lt;code&gt;unpark&lt;/code&gt; method with the parked thread as the target, the parked thread goes back into the &lt;strong&gt;RUNNING&lt;/strong&gt; state.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;join&lt;/code&gt;: The &lt;code&gt;join&lt;/code&gt; method allows a thread to remain in the &lt;strong&gt;WAITING&lt;/strong&gt; state until another thread completes execution.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WaitingStateDemo&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;firstThread&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;firstThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitingStateDemo&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="n"&gt;firstThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;secondThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitingStateDemoInner&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="n"&gt;secondThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
         &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;secondThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;


    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WaitingStateDemoInner&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt;  &lt;span class="nc"&gt;Runnable&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

            &lt;span class="nd"&gt;@Override&lt;/span&gt;
            &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current state of thread 2 is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getState&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;The current state of thread 2 is WAITING

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execution starts from the main method in the above program.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we create &lt;code&gt;firstThread&lt;/code&gt; and invoke its &lt;code&gt;run&lt;/code&gt; method by calling &lt;code&gt;start&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;run&lt;/code&gt; method of &lt;code&gt;firstThread&lt;/code&gt;, we create &lt;code&gt;secondThread&lt;/code&gt; and start it.&lt;/li&gt;
&lt;li&gt;While the processing of &lt;code&gt;firstThread&lt;/code&gt; continues, we call the &lt;code&gt;join&lt;/code&gt; method on &lt;code&gt;secondThread&lt;/code&gt;. The &lt;code&gt;join&lt;/code&gt; method puts &lt;code&gt;firstThread&lt;/code&gt; in the &lt;strong&gt;WAITING&lt;/strong&gt; state until &lt;code&gt;secondThread&lt;/code&gt; has finished its execution.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;firstThread&lt;/code&gt; is waiting for &lt;code&gt;secondThread&lt;/code&gt; to complete, you can get the state of &lt;code&gt;firstThread&lt;/code&gt; from &lt;code&gt;secondThread&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To better understand the &lt;strong&gt;WAITING&lt;/strong&gt; state, consider this scenario:&lt;/p&gt;

&lt;p&gt;You have an onsite interview for a position that has the prospects of an amazing salary. On your arrival for the interview, the secretary tells you to wait until the hiring manager notifies you to come in. At this point, you are in the &lt;strong&gt;WAITING&lt;/strong&gt; state. You cannot proceed to the interview until the hiring manager notifies you to come in.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TIMED-WAITING&lt;/strong&gt;: A thread enters the &lt;strong&gt;TIMED-WAITING&lt;/strong&gt; state when it is waiting for another thread to perform a task within a specified time interval. A thread can be put in the &lt;strong&gt;TIMED-WAITING&lt;/strong&gt; state by calling one of the following methods:

&lt;ul&gt;
&lt;li&gt;thread.sleep(long millis)&lt;/li&gt;
&lt;li&gt;thread.wait(int timeout) or thread.wait(int timeout, int nanos)&lt;/li&gt;
&lt;li&gt;thread.join(long millis)&lt;/li&gt;
&lt;li&gt;LockSupport.parkNanos&lt;/li&gt;
&lt;li&gt;LockSupport.parkUntil&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;You can demonstrate the &lt;strong&gt;TIMED-WAITING&lt;/strong&gt; state by creating a thread from the main thread. You can call the &lt;code&gt;sleep&lt;/code&gt; method on the offshoot thread. This is shown as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TimedWaitingStateDemo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;newThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DemoThread&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
         &lt;span class="n"&gt;newThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// the following sleep gives the ThreadScheduler enough time to start processing newThread&lt;/span&gt;
         &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
             &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current state of newThread is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;newThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getState&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
         &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
             &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
             &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread interrupted "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
         &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DemoThread&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;interrupt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread interrupted "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A thread in the &lt;strong&gt;TIMED-WAITING&lt;/strong&gt; state transitions back to the &lt;strong&gt;RUNNABLE **state when the task it is waiting for completes execution or when the specified time interval elapses. To better understand the **TIMED-WAITING&lt;/strong&gt; state, consider the following scenario:&lt;/p&gt;

&lt;p&gt;You and a friend in another city get tickets to go see a music concert organized in your city. On the day of the concert, you agree with your friend to wait for him until 2 pm. If he does not arrive on/before 2 pm, you can go to the concert alone. Here, you are placed in the &lt;strong&gt;TIMED-WAITING&lt;/strong&gt; state. You can choose to proceed with going to the concert after the specified time (until 2 pm) has elapsed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;BLOCKED&lt;/strong&gt;: A thread transitions into the &lt;strong&gt;BLOCKED&lt;/strong&gt; state when one of the following happens during program execution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the thread is waiting for an I/O operation to complete.&lt;/li&gt;
&lt;li&gt;When the thread is waiting to gain access to the monitor lock on a synchronized object. A monitor lock is a contract that gives a thread sole access to a synchronized object. A synchronized object is an object that is designed to be accessed by only one thread at a time.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To understand the &lt;strong&gt;BLOCKED&lt;/strong&gt; state, consider this scenario:&lt;/p&gt;

&lt;p&gt;You have a meeting with a friend but as you step out of your house, it begins to rain. At this point, you are in a &lt;strong&gt;BLOCKED&lt;/strong&gt; state. You are blocked from meeting with your friend until the rain stops.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TERMINATED&lt;/strong&gt;: A thread enters the &lt;strong&gt;TERMINATED&lt;/strong&gt; state ( sometimes called the &lt;strong&gt;DEAD state&lt;/strong&gt;) when one of the following happens:

&lt;ul&gt;
&lt;li&gt;when it completely executes its task ( as defined in the run method).&lt;/li&gt;
&lt;li&gt;when it terminates due to an error.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TerminatedStateDemo&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This demo is to show the terminated state"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;InterruptedException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
         &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;newThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TerminatedStateDemo&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
         &lt;span class="n"&gt;newThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// this sleep is intended to enable newThread execute completely&lt;/span&gt;
         &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
         &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current state of the new thread is : "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;newThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getState&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;This demo is to show the terminated state
The current state of the new thread is: TERMINATED

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Thread Priorities and Thread Scheduling
&lt;/h3&gt;

&lt;p&gt;The number of services assigned to a given thread is referred to as its priority. Every Java thread has a thread priority. The order in which threads are scheduled is determined by each thread’s thread-priority. Threads with a higher thread-priority are executed before threads with a lower thread priority. In Java, thread-priorities are scaled on a scale of 1 to 10.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 is the lowest priority.&lt;/li&gt;
&lt;li&gt;5 is the standard priority.&lt;/li&gt;
&lt;li&gt;10 is the highest priority.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The priority of the main thread is set to 5 by default. Thread priority is a transferred property. Since every java thread is created off the main thread, every thread starts with a priority of 5. The priority of a thread can be programmed using the &lt;code&gt;setPriority&lt;/code&gt; method. The following enums represent thread priorities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Thread.MIN_PRIORITY&lt;/code&gt; represents the lowest priority.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Thread.NORM_PRIORITY&lt;/code&gt; represents the standard priority.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Thread.MAX_PRIORITY&lt;/code&gt; represents the highest priority.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is a program to understand the Thread-Priority:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThreadPriority&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The thread priority of the running thread is "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getPriority&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;firstThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ThreadPriority&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;secondThread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ThreadPriority&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="n"&gt;firstThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPriority&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;NORM_PRIORITY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;secondThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPriority&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MAX_PRIORITY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;firstThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;secondThread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;The thread priority of the running thread is 5
The thread priority of the running thread is 10

Process finished with &lt;span class="nb"&gt;exit &lt;/span&gt;code 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;It is important to note that thread priorities alone do not guarantee the order in which threads execute.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An Operating System’s Thread Scheduler determines the order with which threads run. A simple implementation of the Thread Scheduler ensures that threads with the same level of priority are assigned processor resources in a round-robin fashion. However, Thread Schedulers are not that simple. Concurrency is a complex concept and the JVM provides utilities that abstract these complexities from the programmer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Starvation and DeadLock
&lt;/h3&gt;

&lt;p&gt;Thread priorities determine the order in which threads are executed. Depending on the Operating System, a steady influx of higher priority threads could cause lower priority threads to stay in the waiting state indefinitely. Such indefinite postponement of thread execution is called starvation.&lt;br&gt;
Closely, related to starvation is the concept of DeadLock. To explain Deadlock, let us say that there are two threads- Thread 1 and Thread 2. Thread 1 cannot execute because it is waiting (directly or indirectly) for Thread 2 to execute. Simultaneously, Thread 2 cannot execute because it is waiting for Thread 1 to execute. Both Thread 1 and Thread 2 are blocking each other, hence the term deadlock. The concept of deadlock concurrency is analogous to a traffic jam.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, you learnt about the different states of Java threads, from time of creation to termination.&lt;br&gt;
I hope that you enjoyed the article and learnt something new too. You can follow me on Twitter on &lt;a href=""&gt;@ehizman&lt;/a&gt;&lt;a href="https://twitter.com/ehizman_tutorEd" rel="noopener noreferrer"&gt;https://twitter.com/ehizman_tutorEd&lt;/a&gt;. This will help me write more valuable content.&lt;/p&gt;

&lt;p&gt;Special shout-outs to &lt;a href="https://twitter.com/nomso_okere" rel="noopener noreferrer"&gt;Confidence Okere&lt;/a&gt;, &lt;a href="https://github.com/toshmanuel" rel="noopener noreferrer"&gt;Oladimeji Omotosho&lt;/a&gt;, and &lt;a href="https://twitter.com/Ozifirebrand" rel="noopener noreferrer"&gt;Ozi Okoroafor&lt;/a&gt; for helping me with reviews and edits.&lt;/p&gt;

&lt;p&gt;I look forward to hearing from you. Always code with ❤️&lt;/p&gt;

</description>
      <category>concurrency</category>
      <category>java</category>
      <category>threads</category>
      <category>multithreading</category>
    </item>
    <item>
      <title>Distinguishing between Concurrency, Parallelism, Asynchronous Programming and Multi-threading</title>
      <dc:creator>Ehis Edemakhiota</dc:creator>
      <pubDate>Mon, 07 Feb 2022 22:50:29 +0000</pubDate>
      <link>https://dev.to/ehizman/distinguishing-between-concurrency-parallelism-asynchronous-programming-and-multi-threading-3d4i</link>
      <guid>https://dev.to/ehizman/distinguishing-between-concurrency-parallelism-asynchronous-programming-and-multi-threading-3d4i</guid>
      <description>&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@martinsanchez?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Martin Sanchez&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/parallel?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Hello Everyone!&lt;/p&gt;

&lt;p&gt;So, I have always wondered how applications handle multiple client requests simultaneously. My curiosity led me to study the following concepts: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Concurrency&lt;/li&gt;
&lt;li&gt;Asynchronous Programming&lt;/li&gt;
&lt;li&gt;Parallelism&lt;/li&gt;
&lt;li&gt;Multi-threading&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During my study, I realized that many people often confuse these concepts. This article aims to simplify these concepts for newbies to programming.&lt;/p&gt;

&lt;p&gt;Having said the lot, let’s jump right into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrency
&lt;/h3&gt;

&lt;p&gt;Concurrency refers to the coordination and management of multiple tasks happening at the same time. The concurrently running tasks can be parts of the same program, methods of the same program or even different programs. The need for more processing speed makes concurrency an important concept in computing. &lt;/p&gt;

&lt;p&gt;Concurrency is visible in everyday computing, for instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can use a word processor program and at the same time listen to music on the same computer.&lt;/li&gt;
&lt;li&gt;You can run multiple instances of the same application on our computer at the same time.&lt;/li&gt;
&lt;li&gt;Multimedia streaming applications allow you to download a file and consume it at the same time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The increased demand for computers that support multiple processors has facilitated concurrent programming. Parallelism makes concurrency possible in a multi-processor system. Concurrency is possible in a single processor system via Context-Switching. Context switching is a system whereby the processor assigns resources to tasks based on two factors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;time slice&lt;/li&gt;
&lt;li&gt;task priority&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Parallelism
&lt;/h3&gt;

&lt;p&gt;Parallelism in computing refers to a paradigm where programs are designed to run concurrently on more than one processor. &lt;br&gt;
The process of parallelism is described as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A problem is broken into discrete tasks that can be solved concurrently&lt;/li&gt;
&lt;li&gt;Each task is further broken down into a series of instructions&lt;/li&gt;
&lt;li&gt;Instructions from each task execute simultaneously on different processors&lt;/li&gt;
&lt;li&gt;A system that coordinates message sharing and synchronization between processes is employed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The increased usage of multi-core processors and GPUs have facilitated parallel processing. Parallelism is utilized heavily in Game Development. GPUs work together with CPUs to increase the throughput of data and the number of concurrent calculations within an application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gb76gmnwg7zr9n6b3px.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gb76gmnwg7zr9n6b3px.gif" alt="parallelProblem.gif" width="660" height="359"&gt;&lt;/a&gt;&lt;br&gt;
image source: &lt;a href="https://hpc.llnl.gov/sites/default/files/parallelProblem.gif" rel="noopener noreferrer"&gt;https://hpc.llnl.gov/sites/default/files/parallelProblem.gif&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Asynchronous Programming
&lt;/h3&gt;

&lt;p&gt;In other to understand the concept of asynchronous programming, you should first understand the concept of synchronous programming.&lt;/p&gt;

&lt;p&gt;In synchronous programming, the processor executes tasks in a sequence. The currently running task must first finish before the processor picks up the next task for execution. In synchronous programming, tasks execute in a blocking fashion(the execution of the next task is blocked until the current task is finished).&lt;/p&gt;

&lt;p&gt;In asynchronous programming, the processor executes tasks simultaneously. The processor does not wait for a task to complete execution before it picks up another task.  This fashion of simultaneous execution is referred to as non-blocking. &lt;/p&gt;

&lt;p&gt;In a computer program, experts advise that a function should be asynchronous when it performs a lengthy operation. A function is designed for asynchrony by including a callback function when invoking them. The asynchronous function executes the callback when it has completed execution as a way of notifying the program about its completion. The execution of the asynchronous function does not block the execution of other functions. &lt;/p&gt;

&lt;p&gt;Asynchronous programs can be single-threaded or multi-threaded. So in asynchronicity, you see dependencies between several parts of a single process&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyj8l52wla2zy3d1acra0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyj8l52wla2zy3d1acra0.png" alt="1642050446527.png" width="714" height="310"&gt;&lt;/a&gt;&lt;br&gt;
image source: &lt;a href="https://www.linkedin.com/pulse/asynchronous-calls-spring-boot-using-async-annotation-omar-ismail/" rel="noopener noreferrer"&gt;https://www.linkedin.com/pulse/asynchronous-calls-spring-boot-using-async-annotation-omar-ismail/&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-threading
&lt;/h3&gt;

&lt;p&gt;A thread is a unit of work or a task. An operation can be split to run on multiple threads. Multi-threading refers to the concurrent/ parallel execution of multiple threads. &lt;/p&gt;

&lt;p&gt;In single-processor systems, Multi-threading is achieved via context-switching.&lt;/p&gt;

&lt;p&gt;In multi-processor systems, Multi-threading is achieved via parallelism.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8k2kelsg0ubpypna32jb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8k2kelsg0ubpypna32jb.png" alt="multithreading.png" width="800" height="401"&gt;&lt;/a&gt;&lt;br&gt;
image source: &lt;a href="https://www.baeldung.com/cs/async-vs-multi-threading" rel="noopener noreferrer"&gt;https://www.baeldung.com/cs/async-vs-multi-threading&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A basic example of multi-threading is downloading two files from two different tabs in a web browser. Each tab uses a new thread to download the requested file. No tab waits for the other one to finish, the downloads run concurrently.&lt;/p&gt;

&lt;p&gt;Multi-threading is breaking down tasks into threads that can be executed by different processors. Asynchronous programs can be multi-threaded or can be single-threaded.&lt;/p&gt;

&lt;p&gt;A program that runs in parallel is also multi-threaded since parallel programs are designed in such a way that they can be broken down into discrete threads that run over multiple processors in a multi-processor system&lt;/p&gt;

&lt;p&gt;To solidify the concepts, I find the following analogies quite useful:&lt;/p&gt;

&lt;p&gt;Parallelism is like hiring two chefs( 2 processors). One cooks rice (one process) using a set of resources(stove and pots) and the other prepares stew (another process) using another set of resources.&lt;/p&gt;

&lt;p&gt;Single-threaded asynchronicity is like setting to cook fried rice. You place the rice on fire to cook and while the rice is cooking, you slice your vegetables and make a sauce. After the rice is cooked, you mix the rice and the sauce&lt;/p&gt;

&lt;p&gt;Multi-threaded asynchronicity is like you setting out to cook fried rice with your brother. You place the rice on fire to cook while he slices the vegetables. You make a sauce while he sets the table for the meal. (All these three tasks involved in the process of cooking rice are interdependent. One process does not wait for the other to finish). When the rice is cooked, you proceed to mix it with the sauce.&lt;/p&gt;




&lt;p&gt;I hope that you enjoyed the tutorial and learnt something new too. You can follow me on Twitter on &lt;a href="https://twitter.com/ehizman_tutorEd" rel="noopener noreferrer"&gt;@ehizman&lt;/a&gt;. This will help me write more valuable content.&lt;/p&gt;

&lt;p&gt;Special shout-outs to &lt;a href="https://twitter.com/nomso_okere" rel="noopener noreferrer"&gt;@nomso_okere&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/adebola-olowose-0b28ab4b/" rel="noopener noreferrer"&gt;Mr Debola Olowose&lt;/a&gt; for helping me with reviews and edits.&lt;/p&gt;

&lt;p&gt;I look forward to hearing from you. &lt;/p&gt;

&lt;p&gt;Always code with ❤️&lt;/p&gt;

</description>
      <category>concurrency</category>
      <category>parallelism</category>
      <category>multithreading</category>
      <category>asynchronousprogramming</category>
    </item>
    <item>
      <title>How the JVM Works</title>
      <dc:creator>Ehis Edemakhiota</dc:creator>
      <pubDate>Mon, 10 Jan 2022 15:01:18 +0000</pubDate>
      <link>https://dev.to/ehizman/how-the-jvm-works-1gfj</link>
      <guid>https://dev.to/ehizman/how-the-jvm-works-1gfj</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;When we write programs (source code) in Java (or in any other compiled language), the execution process is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4q1otoui4wy3jeln017r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4q1otoui4wy3jeln017r.png" alt="compilation process" width="605" height="1231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The source code written by the programmer is converted to machine code or byte code. The machine code or byte code is then executed by the processor of the host machine or by a virtual machine on the host machine that emulates a processor. Languages like C, C++, Go and Rust compiles source code into machine code that is executed by the processor of the host machine. Languages like Java, Kotlin, Groovy and Scala convert source code into an intermediate language known as byte code that is executed by a virtual machine called the JVM(Java Virtual Machine).&lt;/p&gt;

&lt;p&gt;Portability is a benefit of the JVM. The same byte code can run on all machines that have the JVM installed without any need for modification.&lt;br&gt;
The JVM facilitates Java's slogan- "&lt;em&gt;write once, run anywhere&lt;/em&gt;". The JVM is installed as part of one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the Java Development Kit(JDK)&lt;/li&gt;
&lt;li&gt;the Java Runtime Environment(JRE)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The JVM creates space on a host machine to execute Java programs and it operates irrespective of the operating system or platform of the host machine. An instance of the JVM is provided whenever a Java program needs to be executed or run. &lt;/p&gt;

&lt;p&gt;This article gives an insight into how the JVM works. An understanding of how the JVM works is important for Java programmers at all levels. At the end of this article, the reader should be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;describe the JVM and explain how it facilitates "write once, read anywhere"&lt;/li&gt;
&lt;li&gt;describe the components of the JVM&lt;/li&gt;
&lt;li&gt;describe the function of each component of the JVM&lt;/li&gt;
&lt;li&gt;explain the causes of common JVM errors&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Components of the JVM
&lt;/h2&gt;

&lt;p&gt;The JVM consists of the following five components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Class Loader&lt;/li&gt;
&lt;li&gt;Runtime Data Area&lt;/li&gt;
&lt;li&gt;Execution Engine&lt;/li&gt;
&lt;li&gt;Java Native Interface(JNI)&lt;/li&gt;
&lt;li&gt;Native Method Libraries&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following image shows the components of the JVM: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmnu0qfvqcbanij479sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmnu0qfvqcbanij479sv.png" alt="components of the JVM" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us describe each component in detail.&lt;/p&gt;
&lt;h3&gt;
  
  
  Class Loader
&lt;/h3&gt;

&lt;p&gt;The javac compiler converts source code written by the programmer into byte code, which is resident in a &lt;code&gt;.class&lt;/code&gt; file. The class loader loads &lt;code&gt;.class&lt;/code&gt; files into the main memory. In a Java application, the class that contains the main method is first loaded into memory.&lt;/p&gt;

&lt;p&gt;The phases involved in loading a &lt;code&gt;.class&lt;/code&gt; file into memory are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loading Phase&lt;/li&gt;
&lt;li&gt;Linking Phase&lt;/li&gt;
&lt;li&gt;Initialization Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The following image depicts the phases of the JVM class loader: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8recttsdwymqfu5gny48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8recttsdwymqfu5gny48.png" alt="components of the JVM class loader" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us talk about these three phases in a lot more detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Loading Phase&lt;/strong&gt;: A class loader reads the generated byte code in &lt;code&gt;.class&lt;/code&gt; file. It parses the data in the byte code and then stores the following information in an area of the JVM memory called the Method Area:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The kind of object that the parsed byte code represents- if it represents a Class, an Interface or an Enum.&lt;/li&gt;
&lt;li&gt;The fully qualified name of the Class, Interface or Enum that the byte code represents and fully qualified name of the immediate parent&lt;/li&gt;
&lt;li&gt;Information about the fields and methods in the Class, Interface or Enum.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, the JVM creates an object of type &lt;code&gt;Class&lt;/code&gt; and a class loader loads the object of type Class into an area of the JVM memory called the Heap Area using the &lt;code&gt;ClassLoader.loadClass&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;The object of type &lt;code&gt;Class&lt;/code&gt; can be used by a programmer to retrieve class-related information.  Let us do a small demonstration as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ClassInformation {
    public static void main(String[] args) {
        _Native newNative = new _Native("Joe Doe", "SCV070001");
        Class aClass = newNative.getClass();
        String className = aClass.getSimpleName();
        System.out.printf("The class of aClass: %s%n", className);
        System.out.printf("The following methods are defined in %s:%n", className);
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method: methods) {
            System.out.printf("%7s%n", method.getName());
        }
        System.out.printf("The following fields are defined in %s:%n", className);
        Field[] fields = aClass.getDeclaredFields();
        for (Field field:fields) {
            System.out.printf("%7s%n", field.getName());
        }
    }
}
class _Native{
    private String name;
    private String scv;
    public _Native(String name, String scv) {
        this.name = name;
        this.scv = scv;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getScv() {
        return scv;
    }
    public void setScv(String scv) {
        this.scv = scv;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We obtain the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The class of aClass: _Native
The following methods are defined in _Native:
getName
setName
 getScv
 setScv
The following fields are defined in _Native:
   name
    scv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note that for every &lt;code&gt;.class&lt;/code&gt; file only one object of type &lt;code&gt;Class&lt;/code&gt; is produced.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Three types of class loaders exist. They are described as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The Bootstrap ClassLoader:&lt;/strong&gt; The Bootstrap Class Loader is the root class loader. It loads classes in the Java standard packages like &lt;code&gt;java.lang&lt;/code&gt;, &lt;code&gt;java.io&lt;/code&gt;, &lt;code&gt;java.net&lt;/code&gt; and &lt;code&gt;java.util&lt;/code&gt; into the Heap Area. These packages are found in the &lt;em&gt;rt.jar&lt;/em&gt; file and other core libraries present in the &lt;em&gt;$JAVA_HOME/jre/lib&lt;/em&gt; directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The Extension ClassLoader:&lt;/strong&gt; The Extension Class Loader is a child of the Bootstrap Class Loader. The Extension Class Loader loads the extensions of standard Java libraries(which are present in the &lt;em&gt;$JAVA_HOME/jre/lib/ext&lt;/em&gt; directory) into the Heap Area.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. The Application Class Loader:&lt;/strong&gt; The Application Class Loader is a child of the Extension Class Loader. It loads files that are present in the application's classpath into the Heap Area. By default, the application's classpath is set to the current directory of the application.&lt;/p&gt;

&lt;p&gt;The JVM follows the &lt;strong&gt;Delegation-Hierarchy&lt;/strong&gt; principle whenever it tries to find a class. The task of finding a referenced class begins with the Bootstrap Class Loader. If the Bootstrap Class Loader is unable to find a class it delegates the task to the Extension Class Loader and if the Extension Class Loader cannot find the referenced class, it delegates the task to the Application Class Loader. If the Application Class Loader cannot find the referenced class, then a &lt;code&gt;NoClassFoundError&lt;/code&gt; or a &lt;code&gt;ClassNotFoundException&lt;/code&gt; is triggered.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Linking:&lt;/strong&gt; The Linking phase involves the following processes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verification&lt;/li&gt;
&lt;li&gt;Preparation&lt;/li&gt;
&lt;li&gt;Resolution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us describe these processes in more detail:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Verification:&lt;/strong&gt; A ByteCodeVerifier verifies if the semantics of the byte code in a &lt;code&gt;.class&lt;/code&gt; file is valid. If the verification fails, a &lt;code&gt;java.lang.VerifyError&lt;/code&gt; is triggered. For instance, if the code has been built using Java 11 but is being run on a system that has Java 8 installed, the verification process will fail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Preparation:&lt;/strong&gt; The JVM allocates memory for the class variables and initializes the variables to their default values according to their types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Resolution:&lt;/strong&gt; The JVM replaces symbolic references with their actual references which are present in the Method Area of the JVM memory. For instance, if you have references to other classes or to variables or constants defined in other classes they are replaced with their actual references in this phase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Initialization:&lt;/strong&gt; After the processes of loading and linking a class or an interface, the JVM executes the initialization method of a class or an interface(also known as &lt;code&gt;&amp;lt;clinit&amp;gt;&lt;/code&gt;). During the process of executing the initialization method of a class or interface, the following takes place:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the class's constructor or static block is executed &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the class's super-classes are initialized(if they are not already initialized)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the class's variables are assigned their values as specified by the programmer&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Runtime Data Area
&lt;/h3&gt;

&lt;p&gt;The Runtime Data Area consists of five components, which are shown in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40ijxnk8mn25evzmbitp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40ijxnk8mn25evzmbitp.png" alt="Components of the JVM memory" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us look at each one individually:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Method Area:&lt;/strong&gt; In the Method Area, class-related information like the class name, the parent name, method information, and variable information are stored&lt;/p&gt;

&lt;p&gt;If the available memory in the Method Area is not sufficient when loading a program into memory, an &lt;code&gt;OutOfMemoryError&lt;/code&gt; is triggered. The Method Area is created when the JVM starts up. There is only one Method Area per instance of the JVM in a machine. In a multi-threaded environment, all threads shared the same Method Area.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Heap Area:&lt;/strong&gt; All objects and their corresponding instance variables, as well as arrays, are stored in the Heap Area. The Heap Area is also created when the JVM starts up. There is only one Heap Area per instance of the JVM in a machine. In a multi-threaded environment, all threads shared the same Heap Area.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that since the Heap Area and the Method Area are shared resources, the information stored in these areas are not thread-safe.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;3. Stack Area:&lt;/strong&gt; Whenever the JVM creates a new thread, a new Stack Area is created. A Stack Area is divided into blocks called Activation Records or Stack Frames. Whenever a method call occurs, the following are pushed to the next available Stack Frame in a Stack Area:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the local variables of the calling method&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the return address that the called method needs to return to the calling method&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After a method call occurs the corresponding Stack Frame for that method call is popped off the Stack Area.&lt;/p&gt;

&lt;p&gt;When a thread requires a larger stack size than what is available in the Stack Area, a &lt;code&gt;StackOverflowError&lt;/code&gt; is triggered.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the Stack Area is not a shared resource, hence it is thread-safe&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;4. Program Counter(PC) Registers:&lt;/strong&gt; PC registers hold the address of the instruction currently executed by the JVM. After an instruction is executed, the PC register is loaded with the address of the next instruction. In a multi-threaded environment, each thread has its PC register&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Native Method Stacks:&lt;/strong&gt; In a multi-threaded environment, each thread has its Native Method Stack. Information about native methods (methods written in C and C++) are stored in the Native Method Stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution Engine
&lt;/h3&gt;

&lt;p&gt;After the Class Loader loads the byte code into memory, the Execution Engine uses the details in the Runtime Data Area to execute instructions. It executes instructions in a byte code using the following components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interpreter&lt;/li&gt;
&lt;li&gt;Just-In-Time(JIT) compiler&lt;/li&gt;
&lt;li&gt;Garbage Collector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;- Interpreter:&lt;/strong&gt; The Interpreter reads each line of byte code instruction and executes it. The line by line execution of instructions by the interpreter makes it slow. A drawback of the Interpreter is that multiple calls to the same method require interpretation at each time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Just-In-Time(JIT) compiler:&lt;/strong&gt; The Execution Engine uses the JIT compiler for execution whenever it finds repeated code in a byte code. The JIT compiler scans the entire byte code and converts it to native machine code. The JIT compiler replaces repeated code with the direct native code so that re-interpretation is not required. &lt;/p&gt;

&lt;p&gt;The JIT compiler has the following components:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Intermediate Code Generator:&lt;/strong&gt; The Intermediate Code Generator generates intermediate code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Code Optimizer:&lt;/strong&gt; The Code Optimizer optimizes intermediate code generated by the Intermediate Code Generator for better performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Target Code Generator:&lt;/strong&gt; The Target Code Generator converts the optimized intermediate code into native machine code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Profiler:&lt;/strong&gt; The Profiler finds hotspots- code that is executed repeatedly. Whenever repeated code is encountered during execution, the JIT compiler recognizes that the code has a hotspot and it replaces the repeated code with its corresponding direct native code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Garbage Collector:&lt;/strong&gt; The Garbage Collector performs Garbage Collection on the Heap Area. Garbage Collection is the process of removing unreferenced objects from the Heap Area. Garbage Collection involves two phases:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Mark:&lt;/strong&gt; In this process, the Garbage Collector marks unreferenced objects in the Heap Area.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Sweep:&lt;/strong&gt; In this process, the Garbage Collector cleans the object marked during the Mark process.&lt;/p&gt;

&lt;p&gt;Garbage Collection is done automatically by the JVM at regular intervals. The Garbage Collector can also be invoked by calling the &lt;code&gt;System.gc&lt;/code&gt; method but its execution is not guaranteed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Native Interface(JNI)
&lt;/h3&gt;

&lt;p&gt;The Java Native Interface loads Native Methods Libraries into the system memory. The Java Native Interface provides an interface for executing native methods (methods written in C and C++). &lt;/p&gt;

&lt;h3&gt;
  
  
  Native Method Libraries
&lt;/h3&gt;

&lt;p&gt;The Native Method Libraries contain code that is written in native languages: C, C++ and Assembly Language. Native methods are required in cases where we need to write code that is not entirely supported by Java for instance when we need to interact with the system hardware. We add the &lt;code&gt;native&lt;/code&gt; keyword to a method header to indicate that the implementation of the method is available in a native library. After defining a native method, we use the &lt;code&gt;System.loadLibrary&lt;/code&gt; method to load the shared Native Library into the system memory and to make its methods available to Java. Native Libraries usually exist in the form of &lt;em&gt;.so/.dll/.dylib&lt;/em&gt; files&lt;/p&gt;

&lt;h2&gt;
  
  
  Common JVM Errors
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ClassNotFoundException&lt;/code&gt;: The Class Loader triggers a &lt;code&gt;ClassNotFoundException&lt;/code&gt;when the Class it is trying to load a class but does not find a definition for the specified class name. The Class Loader uses &lt;code&gt;Class.forName&lt;/code&gt; method or the &lt;code&gt;ClassLoader.loadClass&lt;/code&gt; method or the &lt;code&gt;ClassLoader.findSystemClass&lt;/code&gt; method to load a class into memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;NoClassDefFoundError&lt;/code&gt;: The JVM triggers this error when a class has been successfully compiled but the Class Loader cannot find the &lt;code&gt;.class&lt;/code&gt; file at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;OutOfMemoryError&lt;/code&gt;: The JVM triggers this error whenever it is out of memory and no more memory can be made available by the Garbage Collector.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;StackOverflowError&lt;/code&gt;: The JVM triggers this error when a thread requires more stack memory than is available in the Stack Area.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we discussed the JVM and its internal components. A good understanding of how the JVM works helps us navigate common JVM related errors like the &lt;code&gt;StackOverflowError&lt;/code&gt; when coding. Also, JVM related questions are popular during Junior and Senior level interviews for Backend Engineers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fef3x8am1lc4t87zcc5mt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fef3x8am1lc4t87zcc5mt.jpg" alt="The End" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for staying till the end. I hoped that you liked the article. Feel free to leave comments in the comment section. You can connect with me on Twitter- &lt;a href="https://twitter.com/ehizman_tutored" rel="noopener noreferrer"&gt;@ehizman_tutored&lt;/a&gt; and also take a look at my other &lt;a href="https://linktr.ee/ehizman" rel="noopener noreferrer"&gt;articles&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Always remember to code with ❤️&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>virtualmachines</category>
    </item>
  </channel>
</rss>
