Chapter fourth the combination of objects
4.1 Building a secure class
4.2 Instance Closure
@ThreadSafepublic class Personset {@GuardedBy ("this") private final set<person> MySet = new Hashset<person> ;(); Public synchronized void Addperson (person p) {Myset.add (P); The public synchronized a Boolean containsperson (person p) {return myset.contains (p); } interface Person {}}
Personset Object MySet is encapsulated, but the security of person is unknown, also called Java Monitor mode
Using private locks to protect objects
public class Privatelock {Private Final object myLock = new Object (); @GuardedBy ("MyLock") widget widget; void SomeMethod () {synchronized (MyLock) {//Access or modify the state of Widget}}}
4.3 Thread-Safe delegation
Immutable domains are thread-safe and can be freely shared and published
A thread-safe container is used to encapsulate a state domain in a class to implement thread safety, and to implement the class's security to other classes in the underlying class, called thread-safety delegation, but if there are multiple interdependent composite objects, the security must also be implemented with locks.
4.4 Adding functionality to existing thread-safe classes
1) Add a method to the original class
2) Extend the original class through inheritance
3) Add functionality through an auxiliary class
Client Lock:
@NotThreadSafeclass BadListHelper <E> { public List<E> list = collections.synchronizedlist (new arraylist<e> ()); Public synchronized boolean putifabsent (e x) { //built-in locks are different for clients boolean absent = !list.contains (x); if (absent) list.add (x); return absent; }} @ThreadSafeclass GoodListHelper <E> { public List<e> list = collections.synchronizedlist (new arraylist<e> ()); public boolean putifabsent (e x) { synchronized (list)   { //list object locks are the same for each client boolean absent = !list.contains (x); if (absent) list.add (x); return absent; } }}
The first built-in class above is who calls who is
Combination:
@ThreadSafepublic class Improvedlist<t> implements list<t> {private final list<t> List; /** * pre:list argument is thread-safe. */Public Improvedlist (list<t> list) {this.list = list;} Public synchronized Boolean putifabsent (T x) {Boolean contains = List.contains (x); if (contains) List.add (x); return!contains; }
The list is introduced as its own member variable, and the built-in lock is the object of the class itself.
Fifth chapter basic Building module
5.1 Synchronization Container class: Vector Hashtable
By encapsulation, each public method is synchronized so that only one thread can access the state of the container at a time, and all access to the state of the container is serialized and inefficient.
The synchronization container classes are thread-safe, but you need to lock yourself to perform complex composite operations, such as iterations. During the iteration, an error occurs if there are other threads modifying the container. Designing a synchronization class container does not solve the problem, so you still need to lock yourself to avoid exceptions in use. However, this can be inefficient because other threads are inaccessible during the entire iteration, and the improved method is container cloning.
Beware of another hidden iteration: "HelloWorld" +set; the ToString () function of the set is called, and iterations are performed inside the function, and the hashcode and equals methods of the container execute the iteration.
5.2 Concurrent Containers: Concurrenthashmap copeonwritearraylist
Concurrenthashmap: Instead of synchronizing each method with the same lock and having only one thread accessing the container at the same time, 20 uses a finer-grained segment lock for greater sharing. The iterator provided does not throw an exception, so it does not need to be locked during the iteration.
Copeonwritearraylist: Used in place of synchronous list, no lock and copy required during iteration. The underlying array is copied each time the modification is completed, and a new container version is republished after the modification.
5.3 Blocking queue and producer-consumer mode
Blockingqueue
5.4 Blocking method and interrupt method
When you call a method in your code that throws a Interruptedexception exception, you become a blocking method, and the wisest choice to catch a Interruptedexception exception is to pass it to the caller. The interruptedexception exception is thrown again. If an exception cannot be thrown, the interrupt method that invokes the thread resumes the interrupt, and the upper layer code sees that an interrupt was thrown. For example:
Public class taskrunnable implements runnable { blockingqueue <task> queue; public void run () { try { Processtask (Queue.take ()); } catch ( Interruptedexception e) { // restore interrupted status thread.currentthread (). Interrupt (); } } void processtask (Task task) { // handle the task } interface task { }}
5.5 Synchronization Tool Class
Latching: Countdownlatch
Futuretask: Asynchronous Event
Signal Volume: Counting semaphore
Fence:
Latching is used to wait for events, and fences are used to wait for processes. Fences can be reset for re-use
Java concurrency programming The combination of object learning notes and the basic building blocks