问题描述
情况大概是这样:class A()
{
属性 a;
void method()
{
启动线程1 {
while(true){
读写a
}
}
启动线程2 {
while(true){
写a
}
}
}
}
对a不上锁的话:
如果a是private,会导致JIT unchain for all on thread11的错误(大概是JIT挂了然后重启了)导致效率大大下降。
如果a是public,没有这个问题
对a上锁没有问题。
实际上对于a的访问的实效性没有什么太大要求,所以比如在C里写是不会有任何问题的。为什么在Android里属性的private和public会导致出现不同的问题?
这个必须是Android的问题。给问题加上了Android标签。
答案很简单,请参考爆栈站的一个问题下面的评论:
concurrency - Android: Reduce time spent in garabage collection"JIT unchain all" means a thread is taking a really long time to suspend, and the VM suspects the thread might be spinning in compiled code, so it's breaking apart some of the compiled code to ensure it hits a safe-point and suspends.
这是Dalvik VM一个小聪明但比较搓的地方⋯
以下备注自用:
JIT unchain all的动作在dvmJitUnchainAll()函数中实现,由Thread.cpp的waitForThreadSuspend()调用。