Java多线程包的Locks一览
Java多线程包提供了Locks,用作线程控制,看到这个名字自然要想起原生的Synchronized关键字,二者有什么优劣呢?
Synchronized在得不到锁时只能等待,但是Locks可以使用tryLock这样的方法
听起来好处也有限,但还是看看Locks的几个API吧
//要求获得锁,会阻塞整个线程
void lock();
//要求获得锁,不然就阻塞,但是可以被其他线程打断
void lockInterruptibly() throws InterruptedException;
//尝试获得锁
boolean tryLock();
//尝试获得锁,不然就等待
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
//释放锁
void unlock();
让我们来看一个面无表情的代码示例:
Lock lock = new ReentrantLock();
lock.lock();
try {
log.info("我获得了锁");
}finally {
lock.unlock();
log.info("我释放了锁");
}
如果加入它引以为傲的tryLock,则是如下写法:
ExecutorService executorService = Executors.newFixedThreadPool(2);
Lock lock = new ReentrantLock();
executorService.submit(() -> {
lock.lock();
try {
log.info("A获得了锁,睡2秒");
Thread.sleep(2000);
}catch (Exception e){
//
}finally {
lock.unlock();
log.info("A释放锁");
}
});
executorService.submit(() -> {
boolean hasLock = lock.tryLock();
try {
if (hasLock){
log.info("B获得了锁");
}else {
log.info("B没获得锁");
}
}finally {
lock.unlock();
log.info("B结束");
}
});
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
输出:
17:29:48.508 [pool-1-thread-2] INFO com.example.demo.LocksTest - B没获得锁
17:29:48.508 [pool-1-thread-1] INFO com.example.demo.LocksTest - A获得了锁,睡2秒
17:29:50.515 [pool-1-thread-1] INFO com.example.demo.LocksTest - A释放锁
在写这篇文章的时候,我不禁在想,已经有了上一篇文章提到的那么多控制多线程并发的工具:CountDownLatch、CyclicBarrier、Semaphore、Phaser,那么为什么还要Locks呢?
之后我看到了一个读写锁接口:读锁只能用来做读操作,当没有写操作进行时,任意线程都可以获取读锁,但写锁只能在没有任何线程读写时,由一个线程获得。
public interface ReadWriteLock {
/**
* Returns the lock used for reading.
*
* @return the lock used for reading
*/
Lock readLock();
/**
* Returns the lock used for writing.
*
* @return the lock used for writing
*/
Lock writeLock();
}
双读线程测试:
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 1; i <=2 ; i ++){
int finalI = i;
executorService.submit(() ->{
Lock readLock = readWriteLock.readLock();
log.info("线程{}准备获取读锁", finalI);
readLock.lock();
try {
log.info("线程{}获取了读锁", finalI);
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readLock.unlock();
log.info("线程{}释放", finalI);
}
});
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
输出:
17:41:56.493 [pool-1-thread-2] INFO com.example.demo.LocksTest - 线程2准备获取读锁
17:41:56.493 [pool-1-thread-1] INFO com.example.demo.LocksTest - 线程1准备获取读锁
17:41:56.499 [pool-1-thread-1] INFO com.example.demo.LocksTest - 线程1获取了读锁
17:41:56.499 [pool-1-thread-2] INFO com.example.demo.LocksTest - 线程2获取了读锁
17:41:59.500 [pool-1-thread-2] INFO com.example.demo.LocksTest - 线程2释放
17:41:59.500 [pool-1-thread-1] INFO com.example.demo.LocksTest - 线程1释放
可见,读锁可以同时获取
那么我如果追加一个写锁:
Thread.sleep(500);
//写1
executorService.submit(() -> {
Lock writeLock = readWriteLock.writeLock();
try {
boolean canWrite = writeLock.tryLock();
log.info("写1线程可以写吗? {}", canWrite);
}finally {
writeLock.unlock();
}
});
//写2
executorService.submit( () -> {
Lock writeLock = readWriteLock.writeLock();
try {
log.info("写2线程试图写入");
boolean canWrite = writeLock.tryLock(4, TimeUnit.SECONDS);
log.info("写2线程可以写吗? {}", canWrite);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writeLock.unlock();
log.info("写2释放");
}
});
等500毫秒为了确保读锁先被获取,之后加入2个写锁,可以看到输出为:
17:47:47.532 [pool-1-thread-2] INFO com.example.demo.LocksTest - 线程2准备获取读锁
17:47:47.532 [pool-1-thread-1] INFO com.example.demo.LocksTest - 线程1准备获取读锁
17:47:47.536 [pool-1-thread-2] INFO com.example.demo.LocksTest - 线程2获取了读锁
17:47:47.536 [pool-1-thread-1] INFO com.example.demo.LocksTest - 线程1获取了读锁
17:47:48.032 [pool-1-thread-3] INFO com.example.demo.LocksTest - 写1线程可以写吗? false
17:47:48.033 [pool-1-thread-4] INFO com.example.demo.LocksTest - 写2线程试图写入
17:47:50.538 [pool-1-thread-1] INFO com.example.demo.LocksTest - 线程1释放
17:47:50.538 [pool-1-thread-2] INFO com.example.demo.LocksTest - 线程2释放
17:47:50.538 [pool-1-thread-4] INFO com.example.demo.LocksTest - 写2线程可以写吗? true
17:47:50.539 [pool-1-thread-4] INFO com.example.demo.LocksTest - 写2释放
Condition
Locks贴心地加入了条件,使得一些操作更加形象,比如你需要维护一个线程安全的栈,且容量只有5,那么
写入操作:
try {
lock.lock();
while (stack.size() == 5) {
notFull.await();
}
stack.push("" + finalI);
notEmpty.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
在notFull.await()时,会执行一个原子性的操作,即释放锁。
释放锁等待弹出操作:
try {
lock.lock();
while (stack.size() == 0){
notEmpty.await();
}
String str = stack.pop();
log.info(str);
notFull.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
使用容量为3的线程测试,首先执行4次入栈,然后执行1次弹出,预期结果是,前三次成功,第四次阻塞,等待弹出后,第四次完成
执行结果如下:
18:45:27.712 [pool-1-thread-1] INFO com.example.demo.LocksTest - 写线程1 尝试获取锁
18:45:27.712 [pool-1-thread-3] INFO com.example.demo.LocksTest - 写线程3 尝试获取锁
18:45:27.716 [pool-1-thread-1] INFO com.example.demo.LocksTest - 写线程1 获取了锁
18:45:27.717 [pool-1-thread-1] INFO com.example.demo.LocksTest - 写线程1 写入完毕
18:45:27.712 [pool-1-thread-4] INFO com.example.demo.LocksTest - 写线程4 尝试获取锁
18:45:27.712 [pool-1-thread-2] INFO com.example.demo.LocksTest - 写线程2 尝试获取锁
18:45:27.717 [pool-1-thread-3] INFO com.example.demo.LocksTest - 写线程3 获取了锁
18:45:27.717 [pool-1-thread-3] INFO com.example.demo.LocksTest - 写线程3 写入完毕
18:45:27.717 [pool-1-thread-4] INFO com.example.demo.LocksTest - 写线程4 获取了锁
18:45:27.717 [pool-1-thread-4] INFO com.example.demo.LocksTest - 写线程4 写入完毕
18:45:27.717 [pool-1-thread-2] INFO com.example.demo.LocksTest - 写线程2 获取了锁
18:45:27.717 [pool-1-thread-2] INFO com.example.demo.LocksTest - 写线程2 被阻塞,因为栈满
18:45:28.214 [pool-1-thread-5] INFO com.example.demo.LocksTest - 4
18:45:28.214 [pool-1-thread-2] INFO com.example.demo.LocksTest - 写线程2 写入完毕
观察最后4行,如预期一致。
在JDK8中,引入了一种新的锁,StampedLock,读写分离的乐观锁,但我在工作中暂时没有遇到过这种需求,因此不再班门弄斧,有兴趣的读者可以自行搜索。
小编是一个有着5年工作经验的java'开发工程师,关于java'编程,自己有做材料的整合,一个完整的java编程学习路线,学习材料和工具,能够进我的群收取,免费送给**723197800**大家,希望你也能凭着自己的努力,成为下一个优秀的程序员。