java - Is there any point in using a volatile long? -


I sometimes use a volatile example variable in cases where I have There are two threads / writing and this does not want to take a lock upwards (or potential deadlock risk); For example, a timer thread is updating an IND from time to time which appears on a square as a gator:

  Public class Mychelles {Private volatile id id; Public MyClass () {Scheduled ExxporterService execService = Executor. NewScheduledThreadPool (1); ExecService.scheduleAtFixedRate (new runnab) (run public zero () {++ id;}}, 0L, 30L, time unit SECONDS); } Public int getId () {return ID; }}  

My question: Given that JLS only guarantees that the 32-bit reading will be atom, whether by any volatile long time ever Is there any point? (I.e. 64-bit).

Alert : Please do not reply by saying that is more than is synchronized is a case pre-optimization Of; I know well how to use when / when is synchronized but there are cases where shaky is better, for example, for use in single-threaded applications For defining spring bean, I favor the instability example variable, because there is no guarantee that spring reference will start the properties of each bean in the main thread.

Not sure I correctly understand your question, but states:

A field can be declared unstable, in this case the Java Memory Model ensures that a compatible value for all thread variables () is viewed. And, perhaps more importantly:

17.7 Non-nuclear treatment dual and long
[.. .]
For the purposes of the Java Programming Language Memory Model, the single writes a non-volatile long or double value to two different types: one in each 32-bit half can be a situation where one Thread looks at the first 32 bits of the 64 bit value by typing one, and another writes 32 bits. The volatile writes long and double values ​​and reads is always atom. writes references and reads are always atom, even if they are implemented as 32 or 64 bit value.

That is, the "whole" variable is protected by the volatile modifier, not just two parts. It inspires me to claim that is longer than to int s for to even of To use even instability, more important is read is non-volatile nuclear / nuclear atom.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -