小编use*_*756的帖子

4
推荐指数
1
解决办法
1131
查看次数

ConcurrentSkipListMap有什么好处?

可能重复:
我什么时候应该使用ConcurrentSkipListMap?

我的意思是如果在java库中有ConcurrentSkipListMap,它有时可能比ConcurrentHashMap更好.我想知道ConcurrentSkipListMap真的好在哪里?

java algorithm collections concurrency concurrenthashmap

3
推荐指数
1
解决办法
3978
查看次数

Double check中的易失物质是什么?

让我们看看经典的双重检查

class Foo {
    private volatile Foo singleton = null;
    public Foo getFooSingleton() {
        if (singleton == null) {
            synchronized(this) {           
                if (singleton == null)
                    singleton = new Foo();
            }
        }
        return singleton;
    }
}
Run Code Online (Sandbox Code Playgroud)

易失性modifire保证在所有线程中都能正确看到"singleton"变量的值.但在当前的例子中我真的需要这个吗?我想不是.所以 - 这就是我看到这个程序以最糟糕的方式运行的方式 - 当一个线程所做的更改被其他人看不到时.

  1. thread one enteres synchronized section并创建singleton
  2. 线程两个enteres同步,同步其当前堆栈值(现在他看到singleton != null),进行第二次检查并退出同步部分.

所以.即使没有挥发性声明,一切都有效,甚至更好=)

java algorithm concurrency synchronization

1
推荐指数
1
解决办法
840
查看次数