Preliminary tutorial for multi-threaded synchronization - Meetux design and use

xiaoxiao2021-03-06  90

Preliminary tutorial for multi-threaded synchronization - Meetux design and use

Mutex is a mutex and is widely used in multi-threaded programming. This paper is implemented as an example with the Mutex implementation of the ConcURRENT toolkit of the Doug LEA in the process as an example. In the Doug Lea's Concurrent Toolkit, Mutex implements the SYNC interface, which is the common interface of all locks (Gate) and condition variables in the Concurrent Toolkit. , Semaphore, Child, Latch, CountDown, ReentrantLock, etc. This also reflects the idea of ​​abstract programming, allowing us to choose different implementations using SYNC without changing the code or changing a small number of code. Here is the definition Sync interface: public interface Sync {public void acquire () throws InterruptedException; // acquiring licensed public boolean attempt (long msecs) throws InterruptedException; // try to obtain a license public void release (); //} through free license Using SYNC can replace the Java Synchronized keyword and provide more flexible synchronization control. Of course, it is not to say that the concurrent kit is a technology independent of Java Synchronized, in fact, the Concurrent kit is also built on the SYNCHRONIZED basis, which can be seen from the following to the Mutex source code. The Synchronized keyword is only in the method or within the code block, and the use of SYNC can span the method even by passing between objects, across the object. This is the Sync and Concurrent toolbots that are more powerful than using Synchronized directly. Note that acquire () and attempt () in Sync will throw InterruptedException, so when using SYNC and its subclasses, call these methods must capture InterruptedException. The Release () method does not throw InterruptedException because Wait () waits for other thread release locks may be called in the acquire () and Attempt () methods. Release () is simplified, directly released the lock, regardless of whether it is really holding. So, you can call RELEASE () this thread that does not have acquire (). Since Release () does not throw InterruptedException, we can call Release () to ensure that the obtained lock can be properly released in the catch or finally clause. For example: class x {sync gate; // ... public void m () {Try {Gate.acquire (); // block untric wardy {// ... method body} finally {Gate.Release () }}}} Catch (interruptedException ex) {// ... evasive action}} Mutex is a non-retrieving mutex. Mutex is widely used in a synchronization environment that requires a Before / After type of a method. The following is the implementation of Mutex in the Concurrent toolkit of Doug LEA.

public class Mutex implements Sync {/ ** The lock status ** / protected boolean inuse_ = false; public void acquire () throws InterruptedException {if (Thread.interrupted ()) throw new InterruptedException (); // (1) synchronized ( {Try {while (inUse_) Wait (); inuse_ = true;} catch (interruptedException ex) {// (2) notify (); throw ex;}}} public synchronized void release () {inuse_ = false; notify ();} public boolean attempt (long msecs) throws InterruptedException {if (Thread.interrupted ()) throw new InterruptedException (); synchronized (this) {if (! inuse_) {inuse_ = true; return true;} else if (msecs <= 0) Return False; else {long waittime = msecs; long start = system.currenttimemillis (); try {for (;;) {Wait (waitTime); if (! inuse_) {inuse_ = true; return true } Else {waittime = msecs - () - start); if (waitTime <= 0) // (3) Return False;}}}}} catch (interruptedException ex) {notify (); throw ex;}}}}} }} Why do you want to check the current thread's interrupt flag in acquire () and Attempt (0 methods. This is to return immediately when the current thread has been interrupted, and will not wait on the lock sign. Calling a thread may generate two different results according to the status of the current thread: When the thread is interrupted during the running process, set the interrupt flag of the current thread to True; if the current thread is blocked in Wait (), SLEEP (), Join (), the interrupt flag of the current thread is emptied while throwing InterruptedException. So the location (2) of the above code is also captured, and then throws InterruptedException again.

The Release () method simply resets the InUse_ flag and informs other threads. The attempt () method is to use Java's Object.Wait (long) for timing, because Object.Wait (long) is not a precise clock, the Attempt (long) method is also a rough timing. Note that the location (3) in the code is returned during the timeout. Mutex is a basic implementation of SYNC, in addition to implementing the method in the SYNC interface, there is no new method. Therefore, Mutex's use and SYNC is exactly the same.

DOUG in the API of the Concurrent package gives an exemplary example of a finely locked list, which is also given here as an example of Mutex and Sync: Class Node {Object Item; Node Next; Mutex Lock = New Mutex ); // Each node holds a lock Node (Object X, Node N) {item = x; Next = n;}} class list {protected node head; // Point to the list // Using Java Synchronized Protect the Head domain // (we can of course use Mutex, but there seems to have the necessary protected synchronized node gethead () {return head;} ​​boolean search (Object x) throws interruptedException {node p = getHead (); if (p == NULL) RETURN FALSE; / / (here more compact, but for the demonstration, various cases are processed separately) p.lock.acquire (); // prime loop by acquiring first lock. // (if THE ACQUIRUPT, The Method Will throw // InterruptedException Now, // So There is no need for any // further cleanup.) for (;;) {ix (x.equals (P.Item)) {p.lock.release (); // Release the lock return true;} else {node nextp = p.next; if (nextp == null) {p.lock.release () ; // Release the last holding lock Return False;} else {try {nextp.lock.acquire (); // Get the lock of the next node before release the current lock} cat (InterruptedException EX) {P.Lock.Release (); // If the acquisition failed, release the current lock throw ex;} p.lock.release (); // Release the lock of the previous node, now hold a new lock P = nextp;

}}}} Synchronized Void Add (Object X) {// Using Synchronized Herfield Head = New Node (X, Head);} // ... Other Similar Traversal and Update Methods ...} Copyright Notice

Is this article helpful? Vote:

Yes

No Vote:

10

0

Discussant: YEHS220 Participation: 78 Experts: 20 Published: 2003-11-28 11:17 am> Calling a thread's interrupt () method may generate two different results according to the status of the current thread: The thread is interrupted during operation, set the interrupt flag of the current thread to true; if the current thread blocks Wait (), Sleep (), Join (), then the current thread's interrupt flag is emptied while throwing InterruptedException. So the location (2) of the above code is also captured, and then throws InterruptedException again.

转载请注明原文地址:https://www.9cbs.com/read-124064.html

New Post(0)