LockSupport和Condition功能和wait/notify类似,都可以完成线程阻塞唤醒或线程通信的功能;其中LockSupport的park和unpark(thread)可以随时随地使用,不需要像wait/notify必须在synchronized中使用;在JUC并发包中也大量的使用LockSupport的功能;本文主要介绍LockSupport和Condition功能和区别;
1. LockSupport
LockSupport 工具可以帮助我们阻塞或唤醒一个线程,也是构建同步组件的基础工具,提供park(阻塞)和unpark(唤醒)功能;
LockSupport.park支持中断唤醒,当线程被调用interrupt时,park会被唤醒;
1.1 LockSupport使用
** park & unpark 使用 **
public void testLockSupport()throws InterruptedException{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
LockSupport.park();
System.out.println("thread被唤醒");
}
});
thread.start();
Thread.sleep(200);
LockSupport.unpark(thread);
}
线程thread内被park,在main中进行unpark操作,线程thread会被从park位置唤醒,接着往下执行;
但是,如果在编程过程中,不小心先执行了unpark,再去执行park时,会发现park一样会被唤醒:
public void testLockSupport(){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
LockSupport.park();
System.out.println("thread被唤醒");
}
});
thread.start();
LockSupport.unpark(thread);
}
执行上面代码会发现,park立即被唤醒;
除了调用unpark唤醒之后,还可以通过线程的interrupt,中断线程:
public void testLockSupport() throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
LockSupport.park();
System.out.println("thread被唤醒");
}
});
thread.start();
Thread.sleep(200);
thread.interrupt();
}
结果输出:thread被唤醒
被中断时,并且没有报错,所以使用LockSupport.park();后一定要判断线程的interrupted(判断之后清除状态) 和isInterrupted(不会清除状态)进行判断;
我们再来试验一下LockSupport进行park时,会对占用的资源做释放吗?
private static String resource= "test_synchronized";
public void testLockSupport() throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
synchronized(resource){
System.out.println("占用到资源");
LockSupport.park();
System.out.println("thread被唤醒");
}
}
});
thread.start();
Thread.sleep(200);
synchronized(resource){
System.out.println("main拿到资源");
}
}
运行可知:不会打印main拿到资源,会造成死锁;
LockSupport实现原理
LockSupport内部也是依赖Unsafe类,LockSupport和使用它的线程关联一个许可 permit,而permit默认是0,当前调用一次unpark时,相当于提供一个许可(将permit改为1),调用一次park时,消费一个许可(将permit改为0);
如果permit是0则阻塞,直到permit被设置为1;
即使调用多次unpark,permit的值也还1,所以多次调用不会累加,一次unpark只能唤醒一次park操作;
另外还有park(blocker)方法,允许传递进来一个blocker:
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
UNSAFE.park(false, 0L);
setBlocker(t, null);
}
private static void setBlocker(Thread t, Object arg) {
// Even though volatile, hotspot doesn't need a write barrier here.
UNSAFE.putObject(t, parkBlockerOffset, arg);
}
private static final long parkBlockerOffset = UNSAFE.objectFieldOffset
(tk.getDeclaredField("parkBlocker"));
而 parkBlocker 属于 Thread中的 volatile Object parkBlocker;
由上可知,会将当前blocker和当前线程绑定起来,方便排查LockSupport阻塞时由哪个资源和线程引起;
2. Condition
Condition是一个接口,提供和wait/notify功能,Condition使用时需要依赖于Lock,ConditionObject是Condition的一个实现,是AbstractQueuedSynchronizer的静态子类,结合着Lock来演示下Condition的使用;
2.1 Condition使用
public static void main(String[] args)throws Exception {
final Lock lock = new ReentrantLock();
final Condition condition = lock.newCondition();
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
});
threadA.start();
Thread.sleep(200);
lock.lock();
condition.signal();
lock.unlock();
}
结果程序正常运行,由上面的小程序可以得出以下结论:
- await/signal必须在lock内执行;
- condition.await会释放锁资源;
- 如果lock.lock()内有synchronized同步代码块,当condition.await时,不会释放syncrhonized同步代码块锁;
Condition也提供了一段使用小程序
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;
public void put(Object x) throws InterruptedException {
lock.lock();
try {
// 没有空间可用
while (count == items.length)
notFull.await();
items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();
Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;
notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}
2.2 Condition原理
Condition的原理以AQS中的ConditionObject来讨论,ConditionObject内部有两个成员变量 Node firstWaiter
和Node lastWaiter
,由这两个变量可知ConditionObject内部是一个链表,Node内部只维护有Node nextWaiter
所以它是一个单向链表;
调用signal时,从firstWaiter找到第一个Node进入唤醒,调用await时,向lastWaiter指向的节点后追加;
调用await源码:
public final void await() throws InterruptedException {
// 判断线程是否被中断
if (Thread.interrupted())
throw new InterruptedException();
// 向链表尾部添加Node
Node node = addConditionWaiter();
// 让当前线程持有的锁进行释放,其他线程在调signal时,能正常获取到锁;
int savedState = fullyRelease(node);
int interruptMode = 0;
// 这个方法内部判断节点类型是:Node.CONDITION时会返回false
while (!isOnSyncQueue(node)) { // ③
// 将当前线程阻塞
LockSupport.park(this); // ②
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
private Node addConditionWaiter() {
// 这里将lastWaiter赋值给t,不会产生并发问题吗?
// 因为调用await之前,必须先拿到lock,所以这里不会有并发问题;
Node t = lastWaiter;
// If lastWaiter is cancelled, clean out.
if (t != null && t.waitStatus != Node.CONDITION) {
unlinkCancelledWaiters();
t = lastWaiter;
}
Node node = new Node(Thread.currentThread(), Node.CONDITION);
if (t == null)
firstWaiter = node;
else
t.nextWaiter = node;
lastWaiter = node;
return node;
}
await源码主要是将 当前线程封装成一个Node.CONDITION类型的node,然后将node添加到链表中;
将当前线程持有的lock锁进行释放,然后当前线程进入阻塞状态,等待被unpark;
调用signal源码:
public final void signal() {
// 判断当前线程是否为持有AQS锁线程
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);
}
private void doSignal(Node first) {
do {
// 如果头节点的下个节点为空,则lastWaiter设置为空
if ( (firstWaiter = first.nextWaiter) == null)
lastWaiter = null;
// 将头节点和next进行断开 也有助于GC
first.nextWaiter = null;
} while (!transferForSignal(first) &&
(first = firstWaiter) != null);
}
final boolean transferForSignal(Node node) {
// 先将firstWaiter节点类型CONDITION改为0 (0是默认值)
if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
return false;
// 将firstWaiter加入到AQS的队列中 (后面讨论AQS会重点讲)
Node p = enq(node);
int ws = p.waitStatus;
// 将节点类型改为SIGNAL:值为-1,后继节点的线程处于等待状态,而当前节点的线程如果释放了同步状态或者被取消,那么就会通知后继节点,让后继节点的线程能够运行
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
// 修改成功,unpark firstWaiter绑定的线程;
LockSupport.unpark(node.thread); // ①
return true;
}
上面①处做了unpark,则await的线程将会被唤醒,接着await源码②处往下执行:
private int checkInterruptWhileWaiting(Node node) {
// 判断线程是否被中断(true)执行 (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT)
// 未中断,则返回 0 ,先来看未被中断的情况;
return Thread.interrupted() ?
(transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
0;
}
// checkInterruptWhileWaiting返回0,会接着执行 ③处代码,进入isOnSyncQueue方法内;
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
final boolean isOnSyncQueue(Node node) {
if (node.waitStatus == Node.CONDITION || node.prev == null)
return false;
if (node.next != null) // If has successor, it must be on queue
return true;
// 会查找node是否在AQS队列中,在signal时已经被放入,所以存在;返回true,结束while循环,完成唤醒;
return findNodeFromTail(node);
}
再来看一下,唤醒过程中,如果线程被中断,会造成什么结果:
private int checkInterruptWhileWaiting(Node node) {
// 判断线程是否被中断(true)执行 (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT)
return Thread.interrupted() ?
// 返回false,响应 REINTERRUPT值;
(transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
0;
}
final boolean transferAfterCancelledWait(Node node) {
if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
enq(node);
return true;
}
// 这里判断为true,最后会返回false;
while (!isOnSyncQueue(node))
Thread.yield();
return false;
}
返回REINTERRUPT = 1,会跳出while循环;
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
接着会执行这段代码;
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
private void reportInterruptAfterWait(int interruptMode)
throws InterruptedException {
if (interruptMode == THROW_IE)
throw new InterruptedException();
else if (interruptMode == REINTERRUPT)
selfInterrupt();
}
// 给当前线程打上中断标识;
static void selfInterrupt() {
Thread.currentThread().interrupt();
}
3. wait/notify
wait和notify是属于Object中的方法,首先就产生一个疑问,为什么wait和notify属于Object而不属于Thread呢?
参考网上答案,后面对synchronized做下深入理解
因为synchronized关键词(设计的初衷可能是为了简化多线程程序的编写)可以加在每个类的方法之上;
每个对象都有一个monitor(因此每个对象都潜在需要线程同步),虚拟机在执行synchronized修饰的代码块时,需要获取该对象的monitor;
而执行wait和notify时,又需要获取monitor,所以wait和notify只能在synchronized内使用;
一个很明显的原因是JAVA提供的锁是对象级的而不是线程级的,每个对象都有锁,通过线程获得;
wait时是否会释放资源
Object obj = new Object();
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
threadA.start();
Thread.sleep(200);
synchronized (obj){
obj.notify();
}
obj.wait会抛出InterruptedException异常,并且会释放syncrhonized(obj)资源;