Enforce consistent lock ordering. Always acquire locks in the exact same sequence across all threads (e.g., always lock
lockAbeforelockB).Use timeouts with
tryLock(). UsetryLock(timeout, unit)so a thread backs off and releases its held locks if it cannot get the next one in time.Avoid nested locks. Avoid acquiring a new lock while already holding another.
What is the difference between process and thread?
Process | Thread |
|---|---|
A process is an independent program in execution with its own isolated memory. | A thread is a lightweight unit of execution within a process, allowing an application to perform multiple tasks concurrently while sharing the same memory space. |
Has its own memory space | Shares memory space |
Heavyweight | Lightweight |
Fault Isolation: High. If one process crashes, other processes remain unaffected. | Low. An unhandled crash or memory corruption in one thread can bring down the entire process. |