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.
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
(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.
Java Multithreading Implementations
(1) Extend Thread and override run():
Output shows that the thread must be started with start(), not run().
(2) Implement Runnable interface:
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.
(3) Example with synchronized (Code B) yields correct results.
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.
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.
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:
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
