JUC —— AQS 的 理解和简单实现

package com.pengshi.ThreadTest;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @description: AQS 中维护一个FIFO 的同步队列,当线程获取同步状态失败后,就会加入到这个CLH同步队列中
 * 我们可以知道ReenteredLock
 * 其中 AQS 可以实现公平和不公平的概念,我们要知道对于 对于AQS 来说 重要的 三个部分是
 * 		state 状态 ,状态 大于1的时候 进行 阻塞 队列
 * 		running Thread 运行线程 ,对于资源而言只有一个运行线程进行访问
 * 		waitingQueue 等待队列,对于被阻塞的队列进入到等待队列等待
 * 	运行逻辑: 线程获取 state,判断 state == 0 为true 进行 抢占资源,state变为1,其它线程获取资源时候,看到state==1进入等待队列
 * 			运行完毕线程退出,state = 0,等待队列出来抢占资源,但是对于非公平锁而言,假如此时 有外部线程访问时候,会进行争夺,对于资源抢占的机会
 * 			那个线程先抢占道位置,把state状态变为1,谁先得到资源,但是如果是公平锁,外部资源访问state == 0的时候,还是加入等待队列,同时
 * 			等待队列是一个FIFO的队列
 *
 *
 * 	下面是可重入的锁,也就是锁的可以进行对于正在运行的线程重复加锁
 * @projectName: Algorithm
 * @see: com.pengshi.ThreadTest
 * @author: pc
 * @createTime: 2022/2/25 18:55
 * @version: 1.0
 */
public class AQSTest {
	public static void main(String[] args) throws InterruptedException {
		final AQSTest aqsTest = new AQSTest();

		Thread t1 = new Thread(() -> {
			try {
				System.out.println("t1 start =============");
				aqsTest.lock();
				System.out.println("t1 get lock ==========");
				aqsTest.showLockThread();
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				aqsTest.unlock();
			}
		});
		t1.setName("T1");

		Thread t2 = new Thread(() -> {
			try {
				System.out.println("t2 start =============");
				aqsTest.lock();
				System.out.println("t2 get lock ==========");
				Thread.sleep(6000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				aqsTest.unlock();
			}
		});
		t2.setName("T2");
		t1.start();
		t2.start();

		Thread.sleep(2000);
		aqsTest.showLockThread();
		t1.join();
		t2.join();

		System.out.println("main exit ...................");
	}

	private Sync sync;
	class Sync extends AbstractQueuedSynchronizer {
		@Override
		protected boolean tryAcquire(int arg) {
			final Thread current = Thread.currentThread();
			/**
			 * state 状态 ,为0时候加入当前线程
			 */
			int c = getState();
			if (c == 0) {
				if (compareAndSetState(0, arg)) {
					setExclusiveOwnerThread(current);
					return true;
				}
			} else if (current == getExclusiveOwnerThread()) {
				// 多次申请资源的时候 ,进行state的记录
				int nextc = c + arg;
				setState(nextc);
				return true;
			}
			return false;
		}

		@Override
		protected boolean tryRelease(int arg) {
			int c = getState() - arg;
			if (Thread.currentThread() != getExclusiveOwnerThread()) {
				throw new IllegalMonitorStateException();
			}
			boolean free = false;
			if (c == 0) {
				free = true;
				setExclusiveOwnerThread(null);
			}
			setState(c);
			return free;
		}

		public void showLockThread() {
			if (getExclusiveOwnerThread() != null) {
				System.out.println("CurrentThread: " + Thread.currentThread().getName());
			}
		}
	}

	AQSTest() {
		sync = new Sync();
	}
	public void lock() {
		sync.acquire(1);
	}
	public void unlock() {
		sync.release(1);
	}
	public void showLockThread() {
		sync.showLockThread();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pengshi12138

加油加油

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值