Concurrent processing of shared data in Java (j2se entry 15)

Source: Internet
Author: User

Concurrent processing of shared data 

Resources that are concurrently accessed by multiple threads are called critical resources.

Multiple ThreadsSimultaneously accessing objectsIt is required that an atomic operation be split when the same resource is operated. (Atomic operations, which cannot be further divided) may result in inconsistent data or incomplete data. To avoid this problem, the method of limiting the access thread is adopted.

Mutex lock mechanism, UseEach object has a monitor (Lock tag)The thread can access this resource only when it has the lock tag. It enters the lock pool without the lock tag. Any object system will create a mutex lock for it, which is to be allocated to the thread to prevent the interruption of atomic operations.The lock of each object can only be allocated to one thread..

1. Synchronized modified code block (Synchronous Code block ),
Public void push (char c ){
Synchronized (this){// Only the lock tag of the current object can access this code block
...
}
}

Lock the object in the brackets. Only the object with the lock mark can execute this code block.

2. Synchronized modification method
PublicSynchronizedVoid push (char c ){
...
}
To lock the current object, this method can be executed only for the object with the lock mark.

Note:The constructor cannot be modified by synchronized.,Static methods can be modified using synchronized.(Locking class objects ),Abstract methods cannot be modified using synchronized.Does not affect subclass overwrite. In this method, you can add synchronized or synchronized to subclass overwrite. Therefore, Java cannot write Waste Code together.

Note: Lock the current object. A code block or method is synchronized (synchronized). When the lock mark of the current object is not allocated, a thread accesses the code block, the lock mark of this object will be released until the thread ends. Other code blocks or method threads that you want to access will enter the lock pool of this object, if the lock tag of the current object is not obtained, the code block or method cannot be accessed. When a thread wants to get the lock mark of an object and enters the lock pool, and the thread holds the lock mark of other objects, the thread will not release the lock mark held by the thread.

Note:The synchronized feature of the method itself is not inherited and can only be overwritten..
The thread is blocked because it has not obtained the lock mark.Lock pool (LockPool). Each object has its own lock pool space for storing the waiting threads. The system determines which thread gets the lock mark and runs it.

Considerations for using mutex locks

Example: for boys and girls, each girl is an object, and each boy is a thread. Every girl has her own lock pool. Every boy may wait in the lock pool.
Class gril {
Public void hand (){
}
Public synchronized void kiss (){
}
}
Class boy extends thread {
Public void run (){
}
}

Note:Read-Only synchronization is not required, and write-only synchronization is not required. synchronization is enabled only when both read and write operations exist..

Note: In the java. Io package, vector and hashtable are thread-safe because each method has synchronized modification. The static method can be used with synchronized and the lock is a class object. However, if vector is JDK 1.0's arraylist and JDK, arraylist is used in actual applications.

Note: internal and external synchronization. Internal synchronization, that is, the method in the class is modified with synchronized. External synchronization means that when only one thread needs to control access, write the method to be controlled in the synchronization code block.

Deadlock

Multiple Threads do not release their own lock tags, but they want to apply for the lock tags owned by other threads, it will cause a deadlock.

The thread that does not obtain the lock mark of the Lock Object cannot access the synchronization method that can only be accessed when the object is marked, but can access the non-synchronous method of the object..

Two methods to deal with deadlocks
Arrange the lock sequence in a unified manner (solve the access to multiple shared resources in different methods)
Method of object 1
Synchronized ()
Synchronized (B)
Method of object 2
Synchronized ()
Synchronized (B)

Java inter-thread Communication(That is, coordination between threads)

Notification pending Mechanism
The space used for inter-thread communication is called the objectWait for the right column (waitPool)The queue also belongs to the object space.


Enter the waiting pool

UseIn the object class, wait () InIn the row status, the thread calls wait (), indicating that the thread will release all its lock tags and CPU usage, and enter the waiting pool of this object.. The status of the wait pool is also blocked, except that the thread releases its lock tag. Only in the synchronization code block that locks the object can the wait () of the object be used, indicating that the thread will release all lock marks and enter the waiting queue, the thread enters the waiting queue status.

A thread enters the synchronization code block that locks an object, calls the wait () method to the object, releases all lock tags of the object, and enters the waiting queue of the object, the other thread obtains the lock mark of the object and enters the code block to call the Y () method for this object (if the notifyall () method is called for this object, will put all threads in the waiting queue), and the thread that calls the method of this object will not release the lock mark (it has no impact on itself ), that is, a thread is released from the waiting queue. To continue running the released thread, the synchronization code block is also entered because the lock mark of the object to access the code block is not obtained, and enter the lock pool of the object and wait for the marked release.

Note:Replace Policy () with policyall () because the system determines which thread to release when calling the notify () method..
Exit wait pool to enter lock pool

Every y (): removes any thread from the waiting pool of the object and puts it in the lock pool where the object remains waiting until the lock mark of the object can be obtained.
Policyall (): removes all threads waiting for that object from the wait pool and puts them in the lock pool. Only the threads in the lock pool can obtain the lock mark of the object, the lock mark allows the thread to continue running from the last point where wait () is interrupted.

Note: Only wait () and notify () can be performed on locked resources ().

1)Wait (): provides the lock and CPU usage;
2) y (): removes any thread from the waiting pool of the object and puts it in the lock pool. The object remains waiting until the lock mark of the object can be obtained.
3) policyall (): removes all threads waiting for that object from the wait pool and puts them in the lock pool. Only the threads in the lock pool can obtain the lock mark of the object, the lock mark allows the thread to continue running from the last point where wait () is interrupted.

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.