Understanding Java Processes vs Threads and Key Concurrency Methods
This article explains the fundamental differences between processes and threads, compares Thread and Runnable in Java, outlines thread lifecycle states, and details common concurrency methods such as sleep, join, yield, interrupt, wait, and synchronized, helping developers write robust multithreaded code.
1. Difference Between Process and Thread
A process has its own independent code and data space (process context); switching between processes incurs significant overhead, and a process contains one or more threads (the smallest unit of resource allocation).
A thread shares the same code and data space within a process, but each thread has its own execution stack and program counter (PC); thread switching overhead is small (the smallest unit of CPU scheduling). Threads, like processes, go through five stages: creation, ready, running, blocked, and terminated.
Multiple processes mean the operating system can run several tasks (programs) simultaneously, while multiple threads mean several execution flows exist within the same program.
In Java, multithreading can be achieved by extending the Thread class or implementing the Runnable interface.
2. Thread vs Runnable
Extending Thread makes resource sharing difficult, whereas implementing Runnable facilitates easy resource sharing.
Advantages of implementing Runnable over extending Thread:
Suitable for multiple threads executing the same code to handle a shared resource.
Avoids Java’s single inheritance limitation.
Improves program robustness; code can be shared among threads while keeping data independent.
Thread pools accept only Runnable or Callable tasks, not classes that extend Thread.
Every Java program starts at least two threads: the main thread and the garbage‑collection thread. Launching a Java program creates a JVM, which corresponds to a process in the operating system.
3. Thread State Transitions
1) New : a thread object has just been created.
2) Runnable (Ready) : after start() is called, the thread enters the runnable pool, waiting to acquire CPU time.
3) Running : the thread obtains CPU and executes its code.
4) Blocked : the thread temporarily relinquishes CPU for reasons such as waiting, synchronization, or I/O. Blocked states include:
Waiting block: the thread calls wait() and is placed in the wait set (releases its lock).
Synchronization block: the thread attempts to acquire a lock that another thread holds, so it is placed in the lock pool.
Other block: the thread calls sleep(), join(), or performs I/O; it remains blocked until the condition ends (note: sleep() does not release the lock).
5) Dead : the thread finishes execution or exits due to an exception.
4. Common Thread Methods
sleep(long millis): puts the thread into a timed blocked state; after the specified milliseconds the thread becomes runnable again. join(): causes the calling thread to wait until the target thread terminates. yield(): hints to the scheduler that the current thread is willing to give up its remaining time slice, moving it back to the runnable state; actual effect is not guaranteed. setPriority(): changes the thread’s priority. interrupt(): sends an interrupt signal to a thread; if the thread is blocked in wait(), sleep(), or join(), it throws InterruptedException. If the exception is caught and ignored, the thread continues. wait() (object method): must be called inside a synchronized block; it releases the object’s lock and places the thread in the wait set until notify() or notifyAll() is invoked or a timeout expires.
5. synchronized Keyword
The synchronized keyword can be applied to:
Instance methods: only one thread can execute any synchronized method of the same object at a time.
Static methods: only one thread can execute any synchronized static method of the class at a time.
Code blocks: synchronized(this){...} locks the current object; synchronized(ClassName.class){...} locks the class object. synchronized is not inherited; subclasses must explicitly declare synchronized methods if needed.
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.
ITFLY8 Architecture Home
ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.
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.
