夜光序言:
你不曾给我一次回眸,我却始终在对你微笑。
正文:
以道御术 / 以术识道
package 使用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 MyLock2 implements Lock{
private Helper helper;
private class Helper extends AbstractQueuedSynchronizer{
//继承一下AbstractQueuedSynchronizer
@Override
protected boolean tryAcquire(int arg) {
//如果第一个线程进来,我们可以拿到锁
//因此我们可以返回true
//如果第二个线程进来,拿不到锁,返回false
//如何判断是第一个线程进来,还是其他线程进来
int state = getState(); // 拿到状态
if (state == 0){
// setState(arg);
if(compareAndSetState(0,arg)){
setExclusiveOwnerThread(Thread.currentThread()); //设置一下
return true;
}
}
return false; // 否则false
}
@Override
protected boolean tryRelease(int arg) {
//锁的获取和释放肯定是一一对应的
//那么调用此方法的线程一定是当前线程
if(Thread.currentThread() != getExclusiveOwnerThread()){
throw new RuntimeException();
}
int state = getState() - arg;
boolean flag = false;
if (state == 0){ //如果为0的话,就表示释放成功了
// setState(0);
setExclusiveOwnerThread(null);
// return true;
flag = true;
}
setState(state); //夜光:逻辑要严谨一点
return flag; //否则大话返回false
}
//我们在这定义一个方法
//Condition接口
Condition newCondition(){
return new ConditionObject(); //构造完成
}
}
@Override
public void lock() {
//这里写一下
helper.acquire(1); //嗯唔
}
@Override
public void lockInterruptibly() throws InterruptedException {
helper.acquireInterruptibly(1); //嗯唔
}
@Override
public boolean tryLock() {
return helper.tryAcquire(1);
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
return helper.tryAcquireNanos(1,unit.toNanos(time));
}
@Override
public void unlock() {
helper.release(1);
}
@Override
public Condition newCondition() {
return helper.newCondition();
}
}
package 使用AQS重写自己的锁;
//夜光
//我们写一个测试类
public class Main {
private int value;
public int next(){
return value++;
}
public static void main(String[] args) {
Main m = new Main();
new Thread(new Runnable() {
@Override
public void run() {
while (true)
//调用
System.out.println(Thread.currentThread().getId()
+ " " + m.next());
}
}).start(); //这里启动
new Thread(new Runnable() {
@Override
public void run() {
while (true)
//调用
System.out.println(Thread.currentThread().getId()
+ " " + m.next());
}
}).start(); //这里启动
}
}
package 使用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 MyLock2 implements Lock{
private Helper helper = new Helper();
private class Helper extends AbstractQueuedSynchronizer{
//继承一下AbstractQueuedSynchronizer
@Override
protected boolean tryAcquire(int arg) {
//如果第一个线程进来,我们可以拿到锁
//因此我们可以返回true
//如果第二个线程进来,拿不到锁,返回false
//如何判断是第一个线程进来,还是其他线程进来
int state = getState(); // 拿到状态
if (state == 0){
// setState(arg);
if(compareAndSetState(0,arg)){
setExclusiveOwnerThread(Thread.currentThread()); //设置一下
return true;
}
}
return false; // 否则false
}
@Override
protected boolean tryRelease(int arg) {
//锁的获取和释放肯定是一一对应的
//那么调用此方法的线程一定是当前线程
if(Thread.currentThread() != getExclusiveOwnerThread()){
throw new RuntimeException();
}
int state = getState() - arg;
boolean flag = false;
if (state == 0){ //如果为0的话,就表示释放成功了
// setState(0);
setExclusiveOwnerThread(null);
// return true;
flag = true;
}
setState(state); //夜光:逻辑要严谨一点
return flag; //否则大话返回false
}
//我们在这定义一个方法
//Condition接口
Condition newCondition(){
return new ConditionObject(); //构造完成
}
}
@Override
public void lock() {
//这里写一下
helper.acquire(1); //嗯唔
}
@Override
public void lockInterruptibly() throws InterruptedException {
helper.acquireInterruptibly(1); //嗯唔
}
@Override
public boolean tryLock() {
return helper.tryAcquire(1);
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
return helper.tryAcquireNanos(1,unit.toNanos(time));
}
@Override
public void unlock() {
helper.release(1);
}
@Override
public Condition newCondition() {
return helper.newCondition();
}
}
package 使用AQS重写自己的锁;
import javax.management.RuntimeErrorException;
import java.util.concurrent.locks.Lock;
//夜光
//我们写一个测试类
public class Main {
private int value;
private MyLock2 lock = new MyLock2();
public int next(){
lock.lock();
//这里加上一个睡眠
try {
Thread.sleep(300);
return value++;
} catch (InterruptedException e) {
throw new RuntimeException();
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
Main m = new Main();
new Thread(new Runnable() {
@Override
public void run() {
while (true)
//调用
System.out.println(Thread.currentThread().getId()
+ " " + m.next());
}
}).start(); //这里启动
new Thread(new Runnable() {
@Override
public void run() {
while (true)
//调用
System.out.println(Thread.currentThread().getId()
+ " " + m.next());
}
}).start(); //这里启动
}
}
只打印出来一个a
package 使用AQS重写自己的锁;
import javax.management.RuntimeErrorException;
import java.util.concurrent.locks.Lock;
//夜光
//我们写一个测试类
public class Main {
private int value;
private MyLock2 lock = new MyLock2();
public int next(){
lock.lock();
//这里加上一个睡眠
try {
Thread.sleep(300);
return value++;
} catch (InterruptedException e) {
throw new RuntimeException();
}finally {
lock.unlock();
}
}
public void a(){
lock.lock();
System.out.println("a");
b();
lock.unlock();
}
public void b(){
lock.lock();
System.out.println("b");
lock.unlock();
}
public static void main(String[] args) {
Main m = new Main();
new Thread(new Runnable() {
@Override
public void run() {
// while (true)
//调用
// System.out.println(Thread.currentThread().getId()
// + " " + m.next());
m.a();
}
}).start(); //这里启动
}
}
- 公平锁:加锁前先查看是否有排队等待的线程,有的话优先处理排在前面的线程,先来先得。
- 非公平锁:线程加锁时直接尝试获取锁,获取不到就自动到队尾等待。
来源:CSDN
作者:GeniusTeam-夜光
链接:https://blog.csdn.net/weixin_41987706/article/details/103685470