Java的LockSupport.park()实现分析
LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了主要的线程同步原语。LockSupport实际上是调用了Unsafe类里的函数,归结到Unsafe里,仅仅有两个函数:
public native void unpark(Thread jthread);
public native void park(boolean isAbsolute, long time);
isAbsolute參数是指明时间是绝对的,还是相对的。
只两个简单的接口,就为上层提供了强大的同步原语。
先来解析下两个函数是做什么的。
unpark函数为线程提供“许可(permit)”,线程调用park函数则等待“许可”。这个有点像信号量,可是这个“许可”是不能叠加的,“许可”是一次性的。
比方线程B连续调用了三次unpark函数,当线程A调用park函数就使用掉这个“许可”,假设线程A再次调用park,则进入等待状态。
注意,unpark函数能够先于park调用。比方线程B调用unpark函数,给线程A发了一个“许可”,那么当线程A调用park时,它发现已经有“许可”了,那么它会立即再继续执行。
实际上,park函数即使没有“许可”,有时也会无理由地返回,这点等下再解析。
park和unpark的灵活之处
上面已经提到,unpark函数能够先于park调用,这个正是它们的灵活之处。
一个线程它有可能在别的线程unPark之前,或者之后,或者同一时候调用了park,那么由于park的特性,它能够不用操心自己的park的时序问题,否则,假设park必需要在unpark之前,那么给编程带来非常大的麻烦!!
考虑一下,两个线程同步,要怎样处理?
在Java5里是用wait/notify/notifyAll来同步的。wait/notify机制有个非常蛋疼的地方是,比方线程B要用notify通知线程A,那么线程B要确保线程A已经在wait调用上等待了,否则线程A可能永远都在等待。编程的时候就会非常蛋疼。
另外,是调用notify,还是notifyAll?
notify仅仅会唤醒一个线程,假设错误地有两个线程在同一个对象上wait等待,那么又悲剧了。为了安全起见,貌似仅仅能调用notifyAll了。
park/unpark模型真正解耦了线程之间的同步,线程之间不再须要一个Object或者其他变量来存储状态,不再须要关心对方的状态。
HotSpot里park/unpark的实现
每一个java线程都有一个Parker实例,Parker类是这样定义的:
class Parker : public os::PlatformParker {
private:
volatile int _counter ;
...
public:
void park(bool isAbsolute, jlong time);
void unpark();
...
}
class PlatformParker : public CHeapObj<mtInternal> {
protected:
pthread_mutex_t _mutex [1] ;
pthread_cond_t _cond [1] ;
...
}
能够看到Parker类实际上用Posix的mutex,condition来实现的。
在Parker类里的_counter字段,就是用来记录所谓的“许可”的。
当调用park时,先尝试直接是否能直接拿到“许可”,即_counter>0时,假设成功,则把_counter设置为0,并返回:
void Parker::park(bool isAbsolute, jlong time) {
// Ideally we'd do something useful while spinning, such
// as calling unpackTime().
// Optional fast-path check:
// Return immediately if a permit is available.
// We depend on Atomic::xchg() having full barrier semantics
// since we are doing a lock-free update to _counter.
if (Atomic::xchg(0, &_counter) > 0) return;
假设不成功,则构造一个ThreadBlockInVM,然后检查_counter是不是>0,假设是,则把_counter设置为0,unlock mutex并返回:
ThreadBlockInVM tbivm(jt);
if (_counter > 0) { // no wait needed
_counter = 0;
status = pthread_mutex_unlock(_mutex);
否则,再推断等待的时间,然后再调用pthread_cond_wait函数等待,假设等待返回,则把_counter设置为0,unlock mutex并返回:
if (time == 0) {
status = pthread_cond_wait (_cond, _mutex) ;
}
_counter = 0 ;
status = pthread_mutex_unlock(_mutex) ;
assert_status(status == 0, status, "invariant") ;
OrderAccess::fence();
当unpark时,则简单多了,直接设置_counter为1,再unlock mutext返回。假设_counter之前的值是0,则还要调用pthread_cond_signal唤醒在park中等待的线程:
void Parker::unpark() {
int s, status ;
status = pthread_mutex_lock(_mutex);
assert (status == 0, "invariant") ;
s = _counter;
_counter = 1;
if (s < 1) {
if (WorkAroundNPTLTimedWaitHang) {
status = pthread_cond_signal (_cond) ;
assert (status == 0, "invariant") ;
status = pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
} else {
status = pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
status = pthread_cond_signal (_cond) ;
assert (status == 0, "invariant") ;
}
} else {
pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
}
}
简而言之,是用mutex和condition保护了一个_counter的变量,当park时,这个变量置为了0,当unpark时,这个变量置为1。
值得注意的是在park函数里,调用pthread_cond_wait时,并没实用while来推断,所以posix condition里的"Spurious wakeup"一样会传递到上层Java的代码里。
关于"Spurious wakeup",參考上一篇blog:http://blog.csdn.net/hengyunabc/article/details/27969613
if (time == 0) {
status = pthread_cond_wait (_cond, _mutex) ;
}
这也就是为什么Java dos里提到,当以下三种情况下park函数会返回:
- Some other thread invokes unpark with the current thread as the target; or
- Some other thread interrupts the current thread; or
- The call spuriously (that is, for no reason) returns.
相关的实现代码在:
http://hg.openjdk.java.net/build-infra/jdk7/hotspot/file/52c4a1ae6adc/src/share/vm/runtime/park.hpp
http://hg.openjdk.java.net/build-infra/jdk7/hotspot/file/52c4a1ae6adc/src/share/vm/runtime/park.cpp
http://hg.openjdk.java.net/build-infra/jdk7/hotspot/file/52c4a1ae6adc/src/os/linux/vm/os_linux.hpp
http://hg.openjdk.java.net/build-infra/jdk7/hotspot/file/52c4a1ae6adc/src/os/linux/vm/os_linux.cpp
其他的一些东东:
Parker类在分配内存时,使用了一个技巧,重载了new函数来实现了cache line对齐。
// We use placement-new to force ParkEvent instances to be
// aligned on 256-byte address boundaries. This ensures that the least
// significant byte of a ParkEvent address is always 0. void * operator new (size_t sz) ;
Parker里使用了一个无锁的队列在分配释放Parker实例:
volatile int Parker::ListLock = 0 ;
Parker * volatile Parker::FreeList = NULL ; Parker * Parker::Allocate (JavaThread * t) {
guarantee (t != NULL, "invariant") ;
Parker * p ; // Start by trying to recycle an existing but unassociated
// Parker from the global free list.
for (;;) {
p = FreeList ;
if (p == NULL) break ;
// 1: Detach
// Tantamount to p = Swap (&FreeList, NULL)
if (Atomic::cmpxchg_ptr (NULL, &FreeList, p) != p) {
continue ;
} // We've detached the list. The list in-hand is now
// local to this thread. This thread can operate on the
// list without risk of interference from other threads.
// 2: Extract -- pop the 1st element from the list.
Parker * List = p->FreeNext ;
if (List == NULL) break ;
for (;;) {
// 3: Try to reattach the residual list
guarantee (List != NULL, "invariant") ;
Parker * Arv = (Parker *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ;
if (Arv == NULL) break ; // New nodes arrived. Try to detach the recent arrivals.
if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) {
continue ;
}
guarantee (Arv != NULL, "invariant") ;
// 4: Merge Arv into List
Parker * Tail = List ;
while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ;
Tail->FreeNext = Arv ;
}
break ;
} if (p != NULL) {
guarantee (p->AssociatedWith == NULL, "invariant") ;
} else {
// Do this the hard way -- materialize a new Parker..
// In rare cases an allocating thread might detach
// a long list -- installing null into FreeList --and
// then stall. Another thread calling Allocate() would see
// FreeList == null and then invoke the ctor. In this case we
// end up with more Parkers in circulation than we need, but
// the race is rare and the outcome is benign.
// Ideally, the # of extant Parkers is equal to the
// maximum # of threads that existed at any one time.
// Because of the race mentioned above, segments of the
// freelist can be transiently inaccessible. At worst
// we may end up with the # of Parkers in circulation
// slightly above the ideal.
p = new Parker() ;
}
p->AssociatedWith = t ; // Associate p with t
p->FreeNext = NULL ;
return p ;
} void Parker::Release (Parker * p) {
if (p == NULL) return ;
guarantee (p->AssociatedWith != NULL, "invariant") ;
guarantee (p->FreeNext == NULL , "invariant") ;
p->AssociatedWith = NULL ;
for (;;) {
// Push p onto FreeList
Parker * List = FreeList ;
p->FreeNext = List ;
if (Atomic::cmpxchg_ptr (p, &FreeList, List) == List) break ;
}
}
总结与扯谈
JUC(Java Util Concurrency)仅用简单的park, unpark和CAS指令就实现了各种高级同步数据结构,并且效率非常高,令人惊叹。
在C++程序猿各种自制轮子的时候,Java程序猿则有非常丰富的并发数据结构,如lock,latch,queue,map等信手拈来。
要知道像C++直到C++11才有标准的线程库,同步原语,但离高级的并发数据结构还有非常远。boost库有提供一些线程,同步相关的类,但也是非常easy的。Intel的tbb有一些高级的并发数据结构,可是国内boost都用得少,更别说tbb了。
最開始研究无锁算法的是C/C++程序猿,可是后来非常多Java程序猿,或者类库開始自制各种高级的并发数据结构,常常能够看到有分析Java并发包的文章。反而C/C++程序猿总是在分析无锁的队列算法。高级的并发数据结构,比方并发的HashMap,没有看到有相关的实现或者分析的文章。在C++11之后,这样的情况才有好转。
由于正确高效实现一个Concurrent Hash Map是非常困难的,要对内存CPU有深刻的认识,并且还要面对CPU不断升级带来的各种坑。
我觉得真正值得信赖的C++并发库,仅仅有Intel的tbb和微软的PPL。
https://software.intel.com/en-us/node/506042 Intel® Threading Building Blocks
http://msdn.microsoft.com/en-us/library/dd492418.aspx Parallel Patterns Library (PPL)
另外FaceBook也开源了一个C++的类库,里面也有并发数据结构。
Java的LockSupport.park()实现分析的更多相关文章
- Java的LockSupport.park()实现分析(转载)
LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了基本的线程同步原语.LockSupport实际上是调用了Unsafe类里的函数,归结到Unsafe里,只有两个函数: p ...
- 4.锁定--Java的LockSupport.park()实现分析
LockSupport类是Java6(JSR166-JUC)引入的一个类,提供了主要的线程同步原语. LockSupport实际上是调用了Unsafe类里的函数.归结到Unsafe里,仅仅有两个函数: ...
- Java并发编程 LockSupport源码分析
这个类比较简单,是一个静态类,不需要实例化直接使用,底层是通过java未开源的Unsafe直接调用底层操作系统来完成对线程的阻塞. package java.util.concurrent.locks ...
- java.util.concurrent包详细分析--转
原文地址:http://blog.csdn.net/windsunmoon/article/details/36903901 概述 Java.util.concurrent 包含许多线程安全.测试良好 ...
- Java线程池使用和分析(二) - execute()原理
相关文章目录: Java线程池使用和分析(一) Java线程池使用和分析(二) - execute()原理 execute()是 java.util.concurrent.Executor接口中唯一的 ...
- Java的LockSupport工具,Condition接口和ConditionObject
在之前我们文章(关于多线程编程基础和同步器),我们就接触到了LockSupport工具和Condition接口,之前使用LockSupport工具来唤醒阻塞的线程,使用Condition接口来实现线程 ...
- java自带的jvm分析工具
http://domark.iteye.com/blog/1924302 这段时间觉得很有必要对java的内存分析工具进行熟悉,这样以后出现机器负载较高,或者反应很慢的时候,我就可以查找原因了.上 ...
- 面试 LockSupport.park()会释放锁资源吗?
(手机横屏看源码更方便) 引子 大家知道,我最近在招人,今天遇到个同学,他的源码看过一些,然后我就开始了AQS连环问. 我:说说AQS的大致流程? 他:AQS包含一个状态变量,一个同步队列--bala ...
- java.util.concurrent各组件分析 一 sun.misc.Unsafe
java.util.concurrent各组件分析 一 sun.misc.Unsafe 说到concurrent包也叫并发包,该包下主要是线程操作,方便的进行并发编程,提到并发那么锁自然是不可缺少的, ...
随机推荐
- 转: requirejs中文api (详细)
RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要目的还是为了代码的模块化.它鼓励在使用脚本时以modul ...
- HTTP BIN测试
http://httpbin.org/ Tracing XML request/responses with JAX-WS: http://stackoverflow.com/questions/19 ...
- 第五节 Code 128 码
128码开始於1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,128码是较为复杂的条码系统,而其所能支援的字元也相对地比其他一维条码来得多,又有不同的编码方式可供交互运用 ...
- @RequestParam
@MVC另外一个特性是其提取和解析请求参数的能力.让我们继续重构上面的方法,并在其中添加@RequestParam注解: @RequestMapping("/accounts/show&qu ...
- c++继承构造子类调用父类构造函数的问题及关于容器指针的问题及当容器里储存指针时,记得要手动释放
看下面的一个问题: class Person { private: string name; public: Person(const string& s=""){ nam ...
- java学习之总结
学的时候忘记写博客,现在java SE学完了一口气把写的代码发了上来没有做什么补充,其中有很多知识漏掉了,学的有点不扎实,接下来写写项目来稳好基础
- HDU OJ 5326 Work( 2015多校联合训练第3场) 并查集
题目连接:戳ME #include <iostream> #include <cstdio> #include <cstring> using namespace ...
- Clojure学习03:数据结构(集合)
Clojure提供了几种强大的数据结构(集合) 一.集合种类 1.vector 相当于数组,如: [2 3 5] , ["ad" "adas" & ...
- python IDE ulipad配置使用
一直认为认为python自带的编辑器idle实在是太简陋了,连显示行号的功能都没有,也非常不好编辑.找了下windows平台下的Ide,发现ulipad很多人推荐使用,就开始安装了. 首先去官网: h ...
- make报错:"/usr/bin/ld: cannot find -lXXX"
在编译php时报错如下: # make ... /usr/bin/ld: cannot find -lltdlcollect2: ld returned 1 exit statusmake: *** ...