Question:
Hello. I'm learning thread synchronization in Java, and as I understood from the book, there are several ways to synchronize: through the synchronized
, through the use of ReentrantLock
locks, and through the wait
/ notify
.
- Have I identified these methods correctly?
- Are these methods self-sufficient? Can each of them be used without resorting to the others?
- Is a combination of these methods impossible/undesirable/acceptable/desirable?
Answer:
- synchronized, wait, notify and notifyAll work together – they are primitives. ReentrantLock is a high-level construct and is implemented through primitives.
- You can create high-level constructs from primitives yourself, but you have to think carefully about the architecture.
- Usually high-level constructs are used rather than notations, but you can mix them up if needed.