Working with Threads-java In a nutshell, 6th

Source: Internet
Author: User

In order to work effectively with multithreaded code, it's important to has the basic

Facts about monitors and locks at your command. This checklist contains the main

Facts that should know:

  • Synchronization is on protecting object state and memory, not code.

    Synchronization is about protecting the state and memory of an object, not code.

  • Synchronization is a cooperative mechanism between threads. One bug can break the cooperative model and has far-reaching consequences.

    Synchronization is a mechanism of inter-threading cooperation, and a bug can break the cooperative model and lead to serious consequences.

  • acquiring a monitor only prevents other threads from acquiring the Monitor-it does not protect the object.

    A thread obtains a monitor, can only prevent other threads from obtaining this monitor---monitor cannot protect objects.

  • Unsynchronized methods can see (and modify) inconsistent state, even while the object's monitor is locked.

    There is no synchronized way to see (and modify) the inconsistent state of the object, even if the object's monitor is locked.

  • Locking an object[] doesn ' t lock the individual objects.

    Locks a object[] array, and cannot lock every object within the array.

  • Primitives is not mutable, so they can ' t (and don ' t need to) be locked.

    Native types are immutable, so they cannot (and do not need to) lock.

  • Synchronized can ' t appear on a method declaration in an interface.

    Synchronization cannot appear on the method of an interface.

  • Inner classes is just syntactic sugar, so locks on Inner classes has no effect on the enclosing class (and Vice Vers a).

    The inner class is just a syntactic sugar, so locking the inner class does not affect the class that contains it (and vice versa).

  • Java ' s locks is reentrant. This means if a thread holding a monitor encounters a synchronized block for the same monitor, it can enter the B Lock.

  • Java
  • The lock is a re-entry lock. This means that if a thread encounters a synchronization block and has a
  • monitor, this thread can still enter this synchronization block.

Working with Threads-java In a nutshell, 6th

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.