日期:2014-05-19  浏览次数:20825 次

多线程 VOLITIEL 关键词求助~
谁能给个建议的DEMO呢。 用MAIN 方法执行。  某个变量加了VOLITIEL就正确输出 不加 就错误了。 
谁能给做个建议DEMO , 谢谢啦~  网上找的 说的一大堆 就是没见DEMO 不好理解。。

------解决方案--------------------
http://www.ticmy.com/?p=5
------解决方案--------------------
引用:
引用:引用:
引用:这个网上太多了,不过个人觉得一定要弄白线程与内容中的VOLITIEL有时不会一样,这一点
你是说 synchronized和volitiel的区别么? 这个我知道。 
关于我说的例子,能帮忙找一个DEMO么。 我怎么觉得找不到。。……


哥们啊 , 我实在是写不出来了。憋了快一个小时了
我能明白那意思 就是不知道怎么写  哎  哎  哎  哎

上一个伪代码

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。
该模式的一个必要条件是:被发布的对象必须是线程安全的,或者是有效的不可变对象(有效不可变意味着对象的状态在发布之后永远不会被修改)。volatile 类型的引用可以确保对象的发布形式的可见性,但是如果对象的状态在发布后将发生更改,那么就需要额外的同步。

看了上面的不知道你明白了没

还有我找出了一个公司代码:
public class xxx implements
xxxx{


// String to show the location this subscriber is listening to.
private String location;
// The application class. We need this so we can insert any events we create

private LogisticsApp app;
/**JMS Destination to send messages to*/
private volatile Destination destination;
/**JMS Template to use to send messages*/
private volatile JmsTemplate template;
public void setDestination(Destination destination) {
this.destination = destination;
}

这个是一个公司的内部代码  用到了volatile 关键字
至于为什么,我没看出来啊!!

算我食言了,我是真的写不出来