Home > Back-end >  Already invalid
Already invalid

Time:12-14

Code from think in Java, the JDK version 1.8, with already locked in a thread in the operation of the values will be thrown PairValuesNotEqualException abnormalities, although the final results of x and y values is the same, will not appear this kind of problem, use synchronized;

Package concurrency. The resource;

Public class ExlicitCriticalSection {

Public static void main (String [] args) throws the Exception {


PairManager pman1=new ExpicitPairManager1 (),
Pman2=new ExpicitPairManager2 ();

CriticalSection. TestApproaces (pman1 pman2);

}
}
Package concurrency. The resource;

Import the Java. Util. Concurrent. The ExecutorService;
Import the Java. Util. Concurrent. Executors;
Import the Java. Util. Concurrent. TimeUnit;

Public class CriticalSection {

The static void testApproaces (PairManager pman1, PairManager pman2) {
The ExecutorService exec=Executors. NewCachedThreadPool ();
PairManipulator pm1=new PairManipulator (pman1), pm2=new PairManipulator (pman2);
PairChecker pcheck1=new PairChecker (pman1), pcheck2=new PairChecker (pman2);
The exec. Execute (pm1);
The exec. Execute (pm2);
The exec. Execute (pcheck1);
The exec. Execute (pcheck2);

Try {
TimeUnit. MILLISECONDS. Sleep (500);

{} catch InterruptedException (e)

System. The out. Println (" Sleep interrupted ");
}
System. The out. Println (" pm1: "+" \ n pm2 "+ + pm1 pm2);
System.exit(0);

}

}


Package concurrency. The resource;
Public class Pair {

Private int x, y;

Public Pair (int x, int y) {

This.=x x;
This. Y=y;

}

Public Pair () {

This (0, 0).

}

Public int getX () {return x; }

Public int getY () {return y; }

Public void incrementX () {x++; }

Public void incrementY () {y++; }

Public String toString () {
The return of "x:" + x + ", y: "+ y;
}

Public class PairValuesNotEqualException extends RuntimeException {

Public PairValuesNotEqualException () {

Super (" Pair of values not equal: "+ Pair. This);
}
}

Public synchronized void checkState () {
If (x!=y) {

Throw new PairValuesNotEqualException ();

}
}
}

Class: PairManager
Package concurrency. The resource;

import java.util.*;
Import the Java. Util. Concurrent. TimeUnit;
Import the Java. Util. Concurrent. Atomic. AtomicInteger;
The abstract class PairManager {

AtomicInteger checkCounter=new AtomicInteger (0);

Protected Pair p=new Pair ();

Private List Storage=Collections. SynchronizedList (new ArrayList ());

Public synchronized Pair getPair () {

Return new Pair (p.g etX (), p.g etY ());
}

Protected void store (Pair) p {

Storage. Add (p);
Try {
TimeUnit. MILLISECONDS. Sleep (50);
} catch InterruptedException (e) {}
}

Public abstract void increment ();
}

Package concurrency. The resource;

Import the Java. Util. Concurrent. The locks. *;
Public class ExpicitPairManager1 extends PairManager {

Private Lock Lock=new already ();

Public void increment () {

The lock. The lock ();
Try {
P.i ncrementX ();
P.i ncrementY ();
Store (getPair ());
} the finally {
The lock, unlock ();
}
}

}

Package concurrency. The resource;

Import the Java. Util. Concurrent. The locks. *;

Public class ExpicitPairManager2 extends PairManager {

Private Lock Lock=new already ();

Public void increment () {



The lock. The lock ();
Try {
P.i ncrementX ();
P.i ncrementY ();
Store (getPair ());
} the finally {
The lock, unlock ();
}


}
}


Package concurrency. The resource;


Public class PairChecker implements Runnable {

Private PairManager PM;

Public PairChecker PairManager (PM) {

This. PM=PM;
}


Public void the run () {

While (true) {
PM. CheckCounter. IncrementAndGet ();
PM. GetPair (). The checkState ();
//System. Out. Println (PM. GetPair (). The toString ());//x and y and the same side of the output data
}
}
}

CodePudding user response:

Rewrite ExpicitPairManager1 and 2 getPair method
 public Pair getPair () {
//Make a copy to keep the the original safe:
The lock. The lock ();
Try {
Pair pp=new Pair (p.g etX (), p.g etY ());
Return pp;
} the finally {
The lock, unlock ();
}
}
  • Related