Fundamentals 8 min read

Understanding Processes, Threads, and Java Synchronization: A Practical Guide

This article explains the concepts of processes and threads, their historical development, and how Java implements multithreading and synchronization using techniques like extending Thread, implementing Runnable, and applying the synchronized keyword to ensure thread safety.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Understanding Processes, Threads, and Java Synchronization: A Practical Guide

Process and Thread Concepts

(1) In traditional operating systems, a program cannot run independently; the basic unit of resource allocation and independent execution is the process. Sequential execution occurs without an OS, while multiprogramming allows concurrent execution, leading to the introduction of the process concept.

(2) Since the 1960s, processes have been the basic unit that can own resources and run independently. In the mid‑1980s, threads (lightweight processes) were introduced to increase concurrency and system throughput, especially on multiprocessor systems where threads improve parallel execution.

—Excerpt from “Computer Operating Systems” by Tang Xiaodan et al., 3rd edition

Process and thread diagram
Process and thread diagram

(3) A process is a running instance of a program, the basic unit for resource allocation and scheduling. In modern designs, a process acts as a container for threads.

(4) A thread (lightweight process) is the smallest unit of execution within a program, representing a single sequential control flow. Multiple threads within a process enable multitasking.

Process‑thread relationship
Process‑thread relationship

Java Multithreading Implementations

(1) Extend Thread and override run():

Thread subclass example
Thread subclass example

Output shows that the thread must be started with start(), not run().

(2) Implement Runnable interface:

Runnable implementation example
Runnable implementation example

Thread Safety and Synchronization

Thread safety means that a class behaves correctly when accessed by multiple threads simultaneously. This is typically achieved with locking mechanisms such as the synchronized keyword, which prevents data inconsistency and race conditions.

(1) synchronized can lock any object or method, defining a critical section.

(2) Example without synchronized (Code A) produces incorrect results because multiple threads modify count concurrently.

Code A without synchronization
Code A without synchronization

(3) Example with synchronized (Code B) yields correct results.

Code B with synchronization
Code B with synchronization
Output of Code B
Output of Code B

When multiple threads invoke MyThread.run() with synchronized, they acquire the object's lock and execute sequentially, though lock contention may occur.

Object Locks: Multiple Objects, Multiple Locks

Each object has its own lock. The following example (Code C) demonstrates two objects ( multiThread1 and multiThread2) each holding a separate lock.

Code C with two objects
Code C with two objects
Output of Code C
Output of Code C

Because each object has its own lock, threads operating on different objects do not block each other.

To coordinate access to a shared static variable count, declare the variable or method as static. All instances then share the same lock.

Code D with static
Code D with static
Output of Code D
Output of Code D

Synchronization vs. Asynchronization

Synchronous ( synchronized) ensures that shared resources are accessed safely, providing atomicity and visibility.

Asynchronous (often termed asynchronized) means operations run independently without coordination.

Example of an asynchronous method:

Asynchronous method example
Asynchronous method example
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Threadmultithreadingprocess
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.