哥们啊 , 我实在是写不出来了。憋了快一个小时了 我能明白那意思 就是不知道怎么写 哎 哎 哎 哎上一个伪代码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 关键字 至于为什么,我没看出来啊!!算我食言了,我是真的写不出来
而且根据网上的说法“官方说法是自动探测 "server class". 该网页介绍,如果当前机器有两个或以上 CPU,并且有2GB以上内存,那么就认为这个机器是 server-class machine” 我应该是默认选用SERVER模式的么。
但是这个问题就先不关注了。 还没开始研究JVM的知识。
就是想问下 如果保证在CLIENT状态下执行, 这个VOLITIEL关键词的缺失可能会带来什么问题呢?
只能说也许有,client模式下JVM对程序所做的优化比server模式少得多
关于我说的例子,能帮忙找一个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 关键字
至于为什么,我没看出来啊!!算我食言了,我是真的写不出来
这个对你应有用……