In learning Java multithreading this piece of knowledge, encountered a lot of questions about when the thread lock release, now summarizes several release locks and do not release the lock operation is as follows:
Do not release locks:
- When a thread executes a synchronous code block or synchronous method, the program calls Thread.Sleep (Long L), Thread.yield () method to suspend execution of the current thread
- When a thread executes a synchronized code block, other threads call the thread suspend () method to suspend the thread, and the thread does not release the lock (Synchronization monitor)
- Try to avoid using suspend () and resume () to control threads
Release Lock:
- Synchronization method for the current thread, end of synchronization code block execution
- The synchronization method of the current thread, the synchronization code block encounters a break, a return terminates the code block, and the method continues execution
- The synchronization method of the current thread, unhandled error and exception occurred in the synchronization code block, resulting in an unexpected end
- The current thread executes the wait () method of the thread object in the synchronization method, the synchronization code block, the current thread pauses, and the lock is released
Java Multi-threaded learning--the operation of releasing and not releasing locks in multiple threads