谷歌搜索时,我发现使用java.io.File#length()可能很慢.
FileChannel有一个size()方法也可用.
在java中有一种有效的方法来获取文件大小吗?
我在java中看到了一些例子,他们在一块代码上做同步来改变一些变量,而这个变量最初被声明为volatile.我在单例类的一个例子中看到,他们将唯一的实例声明为volatile并且他们同步了块初始化那个实例...我的问题是为什么我们在同步它时声明它是不稳定的,为什么我们需要同时做两个?对其他人来说不是其中之一吗?
public class someClass {
volatile static uniqueInstance = null;
public static someClass getInstance() {
if(uniqueInstance == null) {
synchronized(someClass.class) {
if(uniqueInstance == null) {
uniqueInstance = new someClass();
}
}
}
return uniqueInstance;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
java multithreading volatile synchronized double-checked-locking