Private lock Object Mode __java Multithreading

Source: Internet
Author: User
	Private Lock Object Idiom-thwarts Denial-of-service attack
	Private Final Object lock = new Object ();

	public void Foo () {
		synchronized (lock) {
			...}}
	}


Advantages: Prevent other clients from holding the lock object timeout, thus avoiding the client's denial of service attack;

Attention:

1, lock domain is declared final, this can prevent accidentally change its contents, and destroy the lock object;

2. The private lock object mode can only be used in an unconditional thread-safe class (the instance is mutable, but the class has enough internal synchronization, without any external synchronization) , and a conditional thread-safe class (partial method Security) cannot apply this pattern.

3. Private lock object mode is especially suitable for classes designed for inheritance, otherwise subclasses and parent classes are likely to lock each other.



See "Effective Java second Edition," article 70th.

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.