Java中的volatile和synchronized之间的区别(Difference between volatile and synchronized in Java)
我想知道将变量声明为
volatile
并且始终访问Java(特别是J2ME)中的synchronized(this)
块中的变量的区别?根据这篇文章http://www.javamex.com/tutorials/synchronization_volatile.shtml有很多话要说,有很多差异,但也有一些相似之处。
我对这条信息特别感兴趣
...
- 访问一个易失性变量从来没有阻止的可能性:我们只是做一个简单的读或写,所以不同于一个同步块,我们永远不会坚持任何锁;
- 因为访问volatile变量永远不会保持锁定,所以不适合我们想要读取更新写入作为原子操作的情况(除非我们准备“错过更新”);
read-update-write是什么意思? 不写也是更新还是他们只是意味着更新是写取决于读取的信息?
最重要的是,什么时候更适合声明变量,而不是通过
synchronized
访问变量? 对依赖于输入的变量使用volatile
是个好主意? 例如,有一个名为render
的变量通过渲染循环读取并由一个按键事件设置?I am wondering at the difference between declaring a variable as
volatile
and always accessing the variable in asynchronized(this)
block in Java?According to this article http://www.javamex.com/tutorials/synchronization_volatile.shtml there is a lot to be said and there are many differences but also some similarities.
I am particularly interested in this piece of info:
...
- access to a volatile variable never has the potential to block: we're only ever doing a simple read or write, so unlike a synchronized block we will never hold on to any lock;
- because accessing a volatile variable never holds a lock, it is not suitable for cases where we want to read-update-write as an atomic operation (unless we're prepared to "miss an update");
What do they mean by read-update-write? Isn't a write also an update or do they simply mean that the update is a write that depends on the read?
Most of all, when is it more suitable to declare variables
volatile
rather than access them through asynchronized
block? Is it a good idea to usevolatile
for variables that depend on input? For instance, there is a variable calledrender
that is read through the rendering loop and set by a keypress event?
原文:https://stackoverflow.com/questions/3519664
最满意答案
问题在于使用'错误'语法来创建服务
而不是使用:messageServices.factory('MessageService', ['MessageData','localStorageService', 'UserService'], function(MessageData, localStorageService, UserService){ return new MessageService(MessageData, localStorageService, UserService); } );
我不得不使用:
messageServices.factory('MessageService', ['MessageData','localStorageService', 'UserService', function(MessageData, localStorageService, UserService){ return new MessageService(MessageData, localStorageService, UserService); } ]);
我很快就用参数关闭了数组,由于我还在学习,我没有直接看到它,无论如何,我希望我可以帮助那些绊倒的人。
The problem was in using the 'wrong' syntax to create the service
instead of using:messageServices.factory('MessageService', ['MessageData','localStorageService', 'UserService'], function(MessageData, localStorageService, UserService){ return new MessageService(MessageData, localStorageService, UserService); } );
I had to use:
messageServices.factory('MessageService', ['MessageData','localStorageService', 'UserService', function(MessageData, localStorageService, UserService){ return new MessageService(MessageData, localStorageService, UserService); } ]);
I closed the array with parameters to soon, and since I'm still learning I didn't see it directly, anyhow I hope I can help others who stumble upon this.
相关问答
更多-
从$http.get('/cowork/desks')获取响应ajax后,您的$scope.results方法被数据覆盖 因此,当您调用$scope.results方法时,它将抛出,因为$scope.results在控制器范围上下文中不再可用。 TypeError:fn不是函数 修复是你需要将$http.get结果重命名为$http.get的其他内容。 码 $http.get('/cowork/desks').success(function(data, status, headers, config) { ...
-
问题在于使用'错误'语法来创建服务 而不是使用: messageServices.factory('MessageService', ['MessageData','localStorageService', 'UserService'], function(MessageData, localStorageService, UserService){ return new MessageService(MessageData, localStorageService, ...
-
即使 fn: string -> string -> string 和 fn: string -> (fn: string -> string) -> string 两个字符串作为输入,并有一个字符串作为输出。 功能内部的操作不同。 第一个,取一个字符串作为输入,也许有一个本地函数,也取第二个字符串,因为有一个字符串作为输出。 (本地函数中参数中的两个字符串) 第二个将具有更高阶函数,其中一个字符串作为let in函数的输入。 Even though fn: string -> string -> st ...
-
fn.apply(fn, [])使用fn的上下文( this执行函数时的值)以及[] (不带参数)中包含的参数来调用存储在fn的函数。 以这种方式调用apply ,这相当于调用fn.call(fn)似乎很奇怪。 因为fn()将在全局上下文中执行,这意味着函数内的值将是window (假设浏览器环境), fn() 不会是合适的替换。 fn.apply(fn, []) calls the function stored in fn with a context (the value of this while ...
-
我终于想出了如何正确配置提供程序并注入所有依赖项。 以下是您尝试测试提供商时遇到的所有人的代码片段。 这是被测单位: class APIObject constructor: (@api_url) -> get_something: () -> console.log('get_something on ' + @api_url) return @$http({ 'method': 'GET' 'url': @api_url + '/something/' ...
-
我想我知道错误在哪里。 在这一行上: var app = angular.module('starter', ['ionic'],['starter.notestore']) 添加依赖项时,只需要一个数组将其列在第二个参数中,因此它应该是: var app = angular.module('starter', ['ionic', 'starter.notestore']) I think I see where an error is; on this line: var app = angular. ...
-
问题出在您的index.js ,您可以在角度应用程序中定义run方法。 angular .module('pacman', []) // removed dependencies for brevity .run(['$rootScope', '$location', '$modalStack', '$cookies']); 传递给run的数组中的最后一个参数应该是一个函数,但是你忘了传递一个函数 。 改变你的run来添加一些像下面这样的实现,或者如果你没有看到它的任何用处,请删除run ...
-
Visual Studio使用.NET XSLT处理器 - 即XSLT 1.0处理器。 encode-for-uri()函数仅在XPath 2.0 / XQuery中是标准函数 。 XSLT 1.0处理器仅使用XPath 1.0,并且没有实现encode-for-uri()函数和availavle。 要使用此功能,您需要一个XSLT 2.0处理器,例如Saxon.NET或XQSharp。 Visual Studio uses a .NET XSLT processor -- that is XSLT 1.0 ...
-
你需要监视backButtonActions函数,通过jasmine的spyOn方法或通过Jest的jest.spyOn方法 https://facebook.github.io/jest/docs/jest-object.html#jestspyonobject-methodname You need to spy on the backButtonActions function, either via jasmine's spyOn method or via Jest's jest.spyOn me ...
-
你没有显示你的HTML代码。 所以我猜你的HTML代码看起来像这样: 你可以像下面给出的那样尝试:: //div[@class='someclass']/div/* 它将为您提供其他类div中的所有信息,现在,如果您尝试如下所示,可能是您的 ...