A thread can have more than one lock tag, and an object will only give monitor to a thread at most

Source: Internet
Author: User

When a method is decorated with synchronized, it indicates that the method locks the current object.

The effect of adding synchronized to the method and modifying the object with synchronized is consistent.

A thread can have more than one lock tag, and an object will only give monitor to a thread at most.

Synchronized is at the expense of the efficiency at which the program runs, so the scope of the mutex code block should be controlled as much as possible.

The Synchronized property of the method itself is not inherited and can only be overridden.

The blocking that occurs because the thread did not get the lock tag is different from the block in the previous five basic states, called the lock pool.

Each object has its own space for a lock pool that is used to place the thread waiting to run.

Which thread in these threads gets the lock tag is determined by the system.

1 Package tomtexts;2 3  Public classtomtexts_22 {4        Public Static voidMain (String args[])5       {6           intI,a;7           Try{//Monitor a block of code8I=0;9A= the/i;Ten       return; One               } A       Catch(arithmeticexception e) -{//capturing an exception that is removed by 0 -System. out. println ("be removed by 0"); the       } -         } -  -}

A thread can have more than one lock tag, and an object will only give monitor to a thread at most

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.