-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c1c517
commit a35cb0d
Showing
15 changed files
with
320 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.wangyz.aqs; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.AbstractQueuedSynchronizer; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
public class CustomLock implements Lock { | ||
|
||
private Sync sync = new Sync(); | ||
|
||
@Override | ||
public void lock() { | ||
System.out.println(Thread.currentThread().getName() + " ready get lock"); | ||
sync.acquire(1); | ||
System.out.println(Thread.currentThread().getName() + " already got lock"); | ||
} | ||
|
||
@Override | ||
public void lockInterruptibly() throws InterruptedException { | ||
sync.acquireInterruptibly(1); | ||
} | ||
|
||
@Override | ||
public boolean tryLock() { | ||
return sync.tryAcquire(1); | ||
} | ||
|
||
@Override | ||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { | ||
return sync.tryAcquireNanos(1, unit.toNanos(time)); | ||
} | ||
|
||
@Override | ||
public void unlock() { | ||
System.out.println(Thread.currentThread().getName() + " ready release lock"); | ||
sync.release(1); | ||
System.out.println(Thread.currentThread().getName() + " already released lock"); | ||
} | ||
|
||
@Override | ||
public Condition newCondition() { | ||
return sync.newCondition(); | ||
} | ||
|
||
private static class Sync extends AbstractQueuedSynchronizer { | ||
@Override | ||
protected boolean tryAcquire(int arg) { | ||
if (compareAndSetState(0, 1)) { | ||
setExclusiveOwnerThread(Thread.currentThread()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean tryRelease(int arg) { | ||
if (getState() == 0) { | ||
throw new IllegalMonitorStateException(); | ||
} | ||
setExclusiveOwnerThread(null); | ||
setState(0); | ||
return true; | ||
} | ||
|
||
@Override | ||
protected boolean isHeldExclusively() { | ||
return getState() == 1; | ||
} | ||
|
||
Condition newCondition() { | ||
return new ConditionObject(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.wangyz.aqs; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.AbstractQueuedSynchronizer; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
|
||
public class CustomReentrantLock implements Lock { | ||
|
||
private Sync sync = new Sync(); | ||
|
||
@Override | ||
public void lock() { | ||
System.out.println(Thread.currentThread().getName() + " ready get lock"); | ||
sync.acquire(1); | ||
System.out.println(Thread.currentThread().getName() + " already got lock"); | ||
} | ||
|
||
@Override | ||
public void lockInterruptibly() throws InterruptedException { | ||
sync.acquireInterruptibly(1); | ||
} | ||
|
||
@Override | ||
public boolean tryLock() { | ||
return sync.tryAcquire(1); | ||
} | ||
|
||
@Override | ||
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { | ||
return sync.tryAcquireNanos(1, unit.toNanos(time)); | ||
} | ||
|
||
@Override | ||
public void unlock() { | ||
System.out.println(Thread.currentThread().getName() + " ready release lock"); | ||
sync.release(1); | ||
System.out.println(Thread.currentThread().getName() + " already released lock"); | ||
} | ||
|
||
@Override | ||
public Condition newCondition() { | ||
return sync.newCondition(); | ||
} | ||
|
||
private static class Sync extends AbstractQueuedSynchronizer { | ||
@Override | ||
protected boolean tryAcquire(int arg) { | ||
if (compareAndSetState(0, 1)) { | ||
setExclusiveOwnerThread(Thread.currentThread()); | ||
return true; | ||
} else if (getExclusiveOwnerThread() == Thread.currentThread()) { | ||
setState(getState() + 1); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean tryRelease(int arg) { | ||
if (getExclusiveOwnerThread() != Thread.currentThread()) { | ||
throw new IllegalMonitorStateException(); | ||
} | ||
if (getState() == 0) { | ||
throw new IllegalMonitorStateException(); | ||
} | ||
setState(getState() - 1); | ||
if (getState() == 0) { | ||
setExclusiveOwnerThread(null); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
protected boolean isHeldExclusively() { | ||
return getState() > 0; | ||
} | ||
|
||
Condition newCondition() { | ||
return new ConditionObject(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters