发布于2021-03-10 18:17 阅读(552) 评论(0) 点赞(25) 收藏(2)
package com.ruigege.LockSourceAnalysis6;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
public class Test {
final static NonReentrantLock lock = new NonReentrantLock();
final static Condition notFull = lock.newCondition();
final static Condition notEmpty = lock.newCondition();
final static Queue<String> queue = new LinkedBlockingQueue<String>();
final static int queueSize = 10;
public static void main(String[] args) {
Thread producer = new Thread(new Runnable() {
public void run() {
// 获取独占锁
lock.lock();
try {
// (1)如果队列满了,则等待
while(queue.size() == queueSize) {
notEmpty.await();
}
// (2)添加元素到队列
queue.add("ele");
// (3)唤醒消费线程
notFull.signalAll();
}catch(Exception e) {
e.printStackTrace();
}finally {
// 释放锁
lock.unlock();
}
}
});
Thread consumer = new Thread(new Runnable() {
public void run() {
// 获取独占锁
lock.lock();
try {
// 队列空,则等待
while(0 == queue.size()) {
notFull.await();
}
// 消费一个元素
String ele = queue.poll();
// 唤醒生产线程
notEmpty.signalAll();
}catch(Exception e) {
e.printStackTrace();
}finally {
// 释放锁
lock.unlock();
}
}
});
// 启动线程
producer.start();
consumer.start();
}
}
package com.ruigege.LockSourceAnalysis6;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public class NonReentrantLockME implements Lock,java.io.Serializable{
// 内部帮助类
private static class Sync extends AbstractQueueSynchronizer {
// 是否锁已经被持有
protected boolean isHeldExclusively() {
return getState() == 1;
}
// 如果state为0,则尝试获取锁
public boolean tryAcquire(int acquires) {
assert acquires == 1;
if(compareAndSetState(0,1)) {
setExclusiveOwnerThread(Thread.currentThread());
return true;
}
return false;
}
// 尝试释放锁,设置state为0
protected boolean tryRelease(int release) {
assert releases == 1;
if(getState() == 0) {
throw new IllegalMonitorStateException();
}
setExclusiveOwnerThread(null);
setState(0);
return true;
}
// 提供条件变量接口
Condition newConditon() {
return new ConditionObject();
}
}
// 创建一个Sync来做具体的工作
private final Sync sync = new Sync();
public void lock() {
sync.acquire(1);
}
public boolean tryLock() {
return sync.tryAcquire(1);
}
public void unlock() {
sync.release(1);
}
public Condition newCondition() {
return sync.newConditon();
}
public boolean isLocked() {
return sync.isHeldExclusively();
}
public void lockInterruptibly() throws InterruptedException {
sync.acquireInterruptibly(1);
}
public boolean tryLock(long timeout,TimeUnit unit) throws InterruptedException {
return sync.tryAcquireNanos(1,unit.toNanos(timeout));
}
}
https://github.com/ruigege66/ConcurrentJava
原文链接:https://www.cnblogs.com/ruigege0000/p/14483880.html
作者:飞翔公园
链接:http://www.javaheidong.com/blog/article/112245/b17ec25501ad151def85/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!