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