Java Multi-threaded learning--the operation of releasing and not releasing locks in multiple threads

Source: Internet
Author: User

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:

  1. 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
  2. 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)
  3. Try to avoid using suspend () and resume () to control threads


Release Lock:

  1. Synchronization method for the current thread, end of synchronization code block execution
  2. 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
  3. The synchronization method of the current thread, unhandled error and exception occurred in the synchronization code block, resulting in an unexpected end
  4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.