Java achieve a lock

AQS is central to the achievement of java lock, but it is still only just realized it inherited several major method of this class can be rewritten.

1. First, the definition of a synchronous classes, inheritance AQS.

// Here there must be a Sync internal class that implements the lock needs to inherit AQS 
Private static class MySync the extends AbstractQueuedSynchronizer {
/ **
* Use CAS to try to change the synchronization status, grab a lock on the use of cas the current thread state is set to 1, set the current owner thread
* @param arg setting state of the lock must be set to 1
* @return
* /
@Override
protected Boolean to tryAcquire (int Arg) {
IF (Arg = 1!) {
the throw a RuntimeException new new ( "args is not 1");
}
/ / CAS is used to try to change the synchronization status
IF (compareAndSetState (0,1)) {
setExclusiveOwnerThread (Thread.currentThread ());
return to true;
}
return to false;
}

/ **
* lock release operation does not require cas
* @param arg
@Return *
* /
@Override
protected Boolean tryRelease (int Arg) {
IF (getState () == 0) {
the throw new new IllegalMonitorStateException ();
}
// set the lock is not held by any thread
setExclusiveOwnerThread (null);
the setState (0 );
return to true;
}

@Override
protected Boolean isHeldExclusively () {
return getState () ==. 1;
}
}
2. to achieve locking, natural to define my own lock, as follows
/ ** 
* manual locking mechanism is the realization of java
* here mainly to implement several methods: tryLock, Lock, UNLOCK
* /
public class MyLock the implements Lock {
  
  // here just aqs references subclass of
private MySync mySync = new MySync () ;

@Override
public void Lock () {
mySync.acquire (. 1);
}

@Override
public Boolean tryLock () {
return mySync.tryAcquire (. 1);
}

@Override
public Boolean tryLock (Long Time, TimeUnit Unit) throws InterruptedException {
return mySync.tryAcquireNanos (. 1, Time);
}

@Override
public void UNLOCK () {
mySync.release (. 1);
}

@Override
public Condition newCondition() {
return null;
}
@Override
public void lockInterruptibly() throws InterruptedException {

}
}
Well, only two classes to get. Whether Haoshi under the following test. To define a thread, using locks realize their own
the MyThread class the implements the Runnable {public 

MyLock myLock new new MyLock = ();
@Override
public void RUN () {
the try {
myLock.lock ();
TimeUnit.SECONDS.sleep (. 3);
System.out.println (Thread.currentThread () .getName () + "\ t" + " is executed ..." + DateUtil.now ());
} the catch (InterruptedException E) {
e.printStackTrace ();
}
the finally {
myLock.unlock ();
}

}
}

Finally, the following test class, we turn on a small scale 10 threads,
/**
* 测试类
*/
public class Test {
public static void main(String[] args) {
MyThread myThread = new MyThread();

for (int i = 0; i < 10; i++) {
new Thread(myThread,i+"-thread").start();
}
}
}
The output shots are as follows:

The write off a lock of two rows,

 

The effect is obvious


Guess you like

Origin www.cnblogs.com/hd-zg/p/11202203.html