Google guava 中的Monitor
synchronized
自从Java提供了多线程编程,我们经常需要处理这样的情况:在特定的时间,我们需要限制访问,确保只有一个线程访问我们的代码。Java提供了同步关键字synchronized来实现这种访问控制,但是使用synchronized会存在一些问题。第一个问题是,当我们需要调用线程的wait()方法时,我们必须记得去使用while循环。看下面例子,来自guava monitor api上的说明:
public class SafeBox<V> {
private V value;
public synchronized V get() throws InterruptedException {
while (value == null) {
wait();
}
V result = value;
value = null;
notifyAll();
return result;
}
public synchronized void set(V newValue) throws InterruptedException {
while (value != null) {
wait();
}
value = newValue;
notifyAll();
}
}
在这个例子中获取一个值,当值不存在的时候,我们等待。。。有值的时候需要notifyAll()。这里需要注意的是,我们要在while循环中使用wait方法,而不是if。另外用notifyAll而不是notify。
ReentrantLock
在java.util.concurrent包中提供了ReentrantLock,我们使用它来实现上面的场景看看
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class SafeBox<V> {
private final ReentrantLock lock = new ReentrantLock();
private final Condition valuePresent = lock.newCondition();
private final Condition valueAbsent = lock.newCondition();
private V value;
public V get() throws InterruptedException {
lock.lock();
try {
while (value == null) {
valuePresent.await();
}
V result = value;
value = null;
valueAbsent.signal();
return result;
} finally {
lock.unlock();
}
}
public void set(V newValue) throws InterruptedException {
lock.lock();
try {
while (value != null) {
valueAbsent.await();
}
value = newValue;
valuePresent.signal();
} finally {
lock.unlock();
}
}
}
我们依然需要使用while循环,但是有一个好处,我们可以定义两个Condition,这样我们就可以用signal来替代signalAll,这样可能会带来一点性能上的提升。
Monitor
Monitor是一个支持任意布尔条件的同步的抽象,Monitor类是作为ReentrantLock的一个替代,代码中使用Monitor比使用ReentrantLock更不易出错,可读性也更强,并且也没有显著的性能损失,使用Monitor甚至有潜在的性能得到优化,下面我们看看用guava中的Monitor怎么重写上面的代码
package com.hupengcool.guava.concurrency;
import com.google.common.util.concurrent.Monitor;
public class SafeBox<V> {
private final Monitor monitor = new Monitor();
private final Monitor.Guard valuePresent = new Monitor.Guard(monitor) {
public boolean isSatisfied() {
return value != null;
}
};
private final Monitor.Guard valueAbsent = new Monitor.Guard(monitor) {
public boolean isSatisfied() {
return value == null;
}
};
private V value;
public V get() throws InterruptedException {
monitor.enterWhen(valuePresent);
try {
V result = value;
value = null;
return result;
} finally {
monitor.leave();
}
}
public void set(V newValue) throws InterruptedException {
monitor.enterWhen(valueAbsent);
try {
value = newValue;
} finally {
monitor.leave();
}
}
}
可以发现使用guava之后,我们不需要使用while,使用Monitor.Guard定义进入代码快的条件即可,代码变得更加容易阅读,写起来也更加方便。
当我们Monitor的方法返回boolean值的时候,我们在if块中包含try-finally块,确保锁能够释放。
if(monitor.enterIf(guard)){
try{
...work..
}finally{
monitor.leave();
}
}else{
.. monitor not available..
}
当monitor的方法不返回任何值的时候,我们的代码也需要在finally中释放锁
monitor.enter()
try{
...work..
}finally{
monitor.leave();
}
Monitor有几个常用的方法
- enter():进入到当前Monitor,无限期阻塞,等待锁。
- enter(long time, TimeUnit unit):进入到当前Monitor,最多阻塞给定的时间,返回是否进入Monitor。
- tryEnter():如果可以的话立即进入Monitor,不阻塞,返回是否进入Monitor。
- enterWhen(Guard guard):进入当前Monitor,等待Guard的isSatisfied()为true后,继续往下执行 ,但可能会被打断。
- enterIf(Guard guard):如果Guard的isSatisfied()为true,进入当前Monitor。等待获得锁,不需要等待Guard satisfied。
- tryEnterIf(Guard guard):如果Guard的isSatisfied()为true并且可以的话立即进入Monitor,不等待获取锁,也不等待Guard satisfied。
Google guava 中的Monitor的更多相关文章
- Google Guava中的前置条件
前置条件:让方法调用的前置条件判断更简单. Guava在Preconditions类中提供了若干前置条件判断的实用方法,我们建议[在Eclipse中静态导入这些方法]每个方法都有三个变种: check ...
- google guava cache缓存基本使用讲解
代码地址:https://github.com/vikde/demo-guava-cache 一.简介 guava cache是google guava中的一个内存缓存模块,用于将数据缓存到JVM内存 ...
- Java内存缓存-通过Google Guava创建缓存
谷歌Guava缓存 Guava介绍 Guava是Google guava中的一个内存缓存模块,用于将数据缓存到JVM内存中.实际项目开发中经常将一些公共或者常用的数据缓存起来方便快速访问. Guava ...
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...
- [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具
转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...
- [转载]Google Guava官方教程(中文版)
原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,武祖 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] ...
- Google Guava官方教程(中文版)
Google Guava官方教程(中文版) 原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,武祖 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...
- Guava中Predicate的常见用法
Guava中Predicate的常见用法 1. Predicate基本用法 guava提供了许多利用Functions和Predicates来操作Collections的工具,一般在 Iterabl ...
- 使用 Google Guava 美化你的 Java 代码
文章转载自:http://my.oschina.net/leejun2005/blog/172328 目录:[ - ] 1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMP ...
随机推荐
- vs中如何使用NuGet
在vs中如何打开NuGet? 1.工具→NuGet程序包管理器→程序包管理控制台 2.没有的话,就去 工具→扩展和更新 搜索nuget 如果你点击工具,没看到Nuget这些字样,请注意汉化名字为 ...
- android入门 — 多线程(一)
android中的一些耗时操作,例如网络请求,如果不能及时响应,就会导致主线程被阻塞,出现ANR,非常影响用户体验,所以一些耗时的操作,我们会想办法放在子线程中去完成. android的UI操作并不是 ...
- C++ Primer Plus学习:第八章
C++入门第八章:函数探幽 本章将介绍C++语言区别于C语言的新特性.包括内联函数.按引用传递变量.默认的参数值.函数重载以及函数模板. 1 C++内联函数 内联函数是C++为提高程序运行速度所做的一 ...
- erlang调优方法
1. 来自Scaling Erlang的方法 内核调优: # Increase the ipv4 port range: sysctl -w net.ipv4.ip_local_port_range= ...
- 0302借软件工程触IT
没有不想学好的学生,也没有选择计算机软件专业后不想过能进军IT的行业的.就对于自己情况来说,大学选择计算机商业软件专业学习也有一年多时间了,未接触专业知识前IT是一个高大上的向往,在初学C语 ...
- 《高性能JavaScript》学习笔记——日更中
------------------2016-7-20更------------------ 最近在看<高性能JavaScript>一书,里面当中,有讲很多提高js性能的书,正在看的过程中 ...
- 结对项目——fault,error,failure的程序设计
一.结对编程内容: 1.不能触发Fault. 2.触发Fault,但是不触发Error. 3.触发Error,但不触发Failure. 二.结对编程人员 1.周宗耀.周浩: 2.结对截图: 三.结对项 ...
- Eureka服务注册过程
上篇博客<SpringCloud--Eureka服务注册和发现>介绍了Eureka的基本功能,这篇我们来聊聊eureka是如何实现的. 上图是eureka的架构图,Eureka分为Serv ...
- apache 部署web.py
一.安装Mod_wsgi 1.先yum -y install httpd-devel,否则会提示没有apxs 2.如果在make时 wsgi报错apxs:Error: Command failed w ...
- redis 同步化操作
异步化操作是很麻烦的的.不好控.下面介绍个同步化的库bluebird.用法很简单.看下你还子就知道了 const redis = require('redis'); const bluebird = ...