那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader { public volatile Flooble theFlooble; public void initInBackground() { // do lots of stuff theFlooble = new Flooble(); // this is the only write to theFlooble } }public class SomeOtherClass { public void doWork() { while (true) { // do some stuff... // use the Flooble, but only if it is ready if (floobleLoader.theFlooble != null) doSomething(floobleLoader.theFlooble); } } } 原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗?
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader { public volatile Flooble theFlooble; public void initInBackground() { // do lots of stuff theFlooble = new Flooble(); // this is the only write to theFlooble } }public class SomeOtherClass { public void doWork() { while (true) { // do some stuff... // use the Flooble, but only if it is ready if (floobleLoader.theFlooble != null) doSomething(floobleLoader.theFlooble); } } } 原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗?我曾经也想模拟出书中提到的各种异常,但是没有几个能复现的
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader { public volatile Flooble theFlooble; public void initInBackground() { // do lots of stuff theFlooble = new Flooble(); // this is the only write to theFlooble } }public class SomeOtherClass { public void doWork() { while (true) { // do some stuff... // use the Flooble, but only if it is ready if (floobleLoader.theFlooble != null) doSomething(floobleLoader.theFlooble); } } } 原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗? 哦我不纠结了,时机到了自然就懂了 虽然没解决,不过就你回答了两次,大部分分给你了
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader { public volatile Flooble theFlooble; public void initInBackground() { // do lots of stuff theFlooble = new Flooble(); // this is the only write to theFlooble } }public class SomeOtherClass { public void doWork() { while (true) { // do some stuff... // use the Flooble, but only if it is ready if (floobleLoader.theFlooble != null) doSomething(floobleLoader.theFlooble); } } } 原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗? 没完整的代码不是很清楚,但是猜测意思是说两个SomeOtherClass的线程,一个线程还在初始化Flooble,另一个线程提前引用了Flooble,第二个线程就把flooble当作了null。这样就会初始化两次,就会有错误。
volatile主要用处是在并发情况下,保证每个线程拿到的数据是一致的,也就是说是对加锁,单例的一种更加严格的保证。
在你每次需要这个数据的时候,它都会去内存中取,如果更新了数据,它会实时的更新内存中的数据。
所以就保证了多线程下的数据一致性。如果将变量用volatile修饰,将会告诉jvm,不会保存这个变量的私有本地变量值。
这样导致的结果就是效率会低一点,因为每次都会去比较,都会去重新获取。
这是我的一些个人见解,还不是很透彻,仅供参考,如果楼主有了更加深入的理解,也可以一起交流学习。
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader {
public volatile Flooble theFlooble; public void initInBackground() {
// do lots of stuff
theFlooble = new Flooble(); // this is the only write to theFlooble
}
}public class SomeOtherClass {
public void doWork() {
while (true) {
// do some stuff...
// use the Flooble, but only if it is ready
if (floobleLoader.theFlooble != null)
doSomething(floobleLoader.theFlooble);
}
}
}
原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗?
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader {
public volatile Flooble theFlooble; public void initInBackground() {
// do lots of stuff
theFlooble = new Flooble(); // this is the only write to theFlooble
}
}public class SomeOtherClass {
public void doWork() {
while (true) {
// do some stuff...
// use the Flooble, but only if it is ready
if (floobleLoader.theFlooble != null)
doSomething(floobleLoader.theFlooble);
}
}
}
原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗?我曾经也想模拟出书中提到的各种异常,但是没有几个能复现的
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader {
public volatile Flooble theFlooble; public void initInBackground() {
// do lots of stuff
theFlooble = new Flooble(); // this is the only write to theFlooble
}
}public class SomeOtherClass {
public void doWork() {
while (true) {
// do some stuff...
// use the Flooble, but only if it is ready
if (floobleLoader.theFlooble != null)
doSomething(floobleLoader.theFlooble);
}
}
}
原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗?
哦我不纠结了,时机到了自然就懂了 虽然没解决,不过就你回答了两次,大部分分给你了
那关于安全发布你知道些什么吗? 看下面这段代码public class BackgroundFloobleLoader {
public volatile Flooble theFlooble; public void initInBackground() {
// do lots of stuff
theFlooble = new Flooble(); // this is the only write to theFlooble
}
}public class SomeOtherClass {
public void doWork() {
while (true) {
// do some stuff...
// use the Flooble, but only if it is ready
if (floobleLoader.theFlooble != null)
doSomething(floobleLoader.theFlooble);
}
}
}
原文作者说如果 theFlooble 引用不是 volatile 类型,doWork() 中的代码在解除对 theFlooble 的引用时,将会得到一个不完全构造的 Flooble。 这句话我看不懂,什么叫做一个不完全构造的Flooble,这段代码不完整,能帮我改一下让我可以在程序中模拟出异常的状况吗?
没完整的代码不是很清楚,但是猜测意思是说两个SomeOtherClass的线程,一个线程还在初始化Flooble,另一个线程提前引用了Flooble,第二个线程就把flooble当作了null。这样就会初始化两次,就会有错误。
加锁会影响效率,而在非原子性操作上,使用volatile并不能保证并发的线程安全。那有什么好的处理方式吗
加锁会影响效率,而在非原子性操作上,使用volatile并不能保证并发的线程安全。那有什么好的处理方式吗
现代处理器都有“检查并交换”的指令,检查变量的值若等于A,则代换为B。整个过程是原子性的。在机器码的层面上实现了线程安全。具体到java,尽量用原子类型,名字都是AtomicXXX。推荐看看《java并发实战》这本书。基本上这些问题都讲过了。