Java Multithreading: Concepts, Thread Creation, Synchronization, Thread Pools, and Deadlock Prevention
This article explains core Java multithreading concepts, covering the differences between parallel and concurrent execution, thread vs process, daemon threads, various ways to create threads, thread states, synchronization mechanisms, thread‑pool types, lock escalation, deadlock causes and prevention, as well as ThreadLocal and atomic utilities.
35. Difference between parallel and concurrent Parallel execution means multiple events occur at the same instant, while concurrency means multiple events occur within overlapping time intervals; parallelism can span multiple processors, whereas concurrency occurs on a single processor.
36. Thread vs. Process A process is the basic unit of execution and resource allocation, containing at least one thread; threads share the same memory space of a process, allowing faster context switches.
37. Daemon Thread A daemon (service) thread runs in the background to support other user threads.
38. Ways to create a thread
Inherit from Thread and override run(), then instantiate and call start().
Implement Runnable, pass an instance to a Thread constructor, and call start().
Implement Callable, wrap it with FutureTask, pass the task to a Thread, start the thread, and retrieve the result via FutureTask.get().
39. Runnable vs. Callable Runnable.run() returns void, while Callable.call() returns a generic result and can be used with Future to obtain the outcome.
40. Thread states Created, Ready, Running, Blocked, and Terminated.
41. sleep() vs. wait() Thread.sleep() pauses the current thread without releasing the monitor; Object.wait() releases the monitor and places the thread into the object's wait set.
42. notify() vs. notifyAll() notify() wakes a single waiting thread, while notifyAll() wakes all waiting threads, moving them to the lock pool for contention.
43. run() vs. start() Calling run() executes the code in the current thread like a normal method; start() creates a new thread, places it in the Ready state, and eventually invokes run() in that new thread.
44. Thread‑pool creation methods newFixedThreadPool(int nThreads): fixed‑size pool with a maximum number of threads. newCachedThreadPool(): creates new threads as needed and reuses idle ones. newSingleThreadExecutor(): single‑threaded executor that guarantees sequential task execution. newScheduledThreadPool(int corePoolSize): pool that can schedule tasks with delays or periodic execution.
45. Thread‑pool states Running, ShutDown, Stop, Tidying, Terminated.
46. submit() vs. execute() submit() accepts Callable or Runnable and returns a Future for result retrieval; execute() only accepts Runnable and returns no result.
47. Ensuring thread safety Achieved through atomicity (e.g., synchronized, Atomic classes), visibility (e.g., volatile, synchronized), and ordering (happens‑before guarantees).
48. Lock escalation Java locks progress from no‑lock → biased lock → lightweight lock → heavyweight lock as contention increases; escalation is one‑way.
49. Deadlock A situation where two or more threads wait indefinitely for resources held by each other, causing the system to halt.
50. Preventing deadlock Break any of the four necessary conditions: mutual exclusion, hold‑and‑wait, no preemption, circular wait.
51. ThreadLocal Provides thread‑confined variables; useful for per‑thread state but can cause memory leaks if not cleared in long‑lived thread pools.
52. synchronized implementation Uses the object header’s mark word as a lock; different lock types (biased, lightweight, heavyweight) are applied based on contention.
53. synchronized vs. volatile synchronized provides mutual exclusion and visibility; volatile only guarantees visibility without atomicity.
54. synchronized vs. Lock Lock is a Java class offering explicit lock acquisition, try‑lock, and fairness options, while synchronized is a JVM keyword with automatic release.
55. synchronized vs. ReentrantLock ReentrantLock offers timeout, interruptible lock acquisition, and richer introspection; synchronized is simpler and automatically releases on method exit or exception.
56. Atomic classes principle They rely on low‑level sun.misc.Unsafe CAS operations to provide lock‑free, thread‑safe updates to single variables.
End of article.
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 Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java 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.
