I haven't ever :( I even haven't ever heard about it before reading this post on stackoverflow.com: http://stackoverflow.com/questions/698964/checking-if-a-clientsocket-has-disconnected-in-java-hangs
And here is something I've found on the net:
And here is something I've found on the net:
- http://stackoverflow.com/questions/106591/do-you-ever-use-the-volatile-keyword-in-java
- http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.4
8.3.1.4.
volatile
FieldsThe Java programming language allows threads to access shared variables (§17.1). As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables.The Java programming language provides a second mechanism,volatile
fields, that is more convenient than locking for some purposes.A field may be declaredvolatile
, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4).
Example 8.3.1.4-1.volatile
FieldsIf, in the following example, one thread repeatedly calls the methodone
(but no more thanInteger.MAX_VALUE
times in all), and another thread repeatedly calls the methodtwo
:class Test { static int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } }then methodtwo
could occasionally print a value forj
that is greater than the value ofi
, because the example includes no synchronization and, under the rules explained in §17.4, the shared values ofi
andj
might be updated out of order.One way to prevent this out-or-order behavior would be to declare methodsone
andtwo
to besynchronized
(§8.4.3.6):class Test { static int i = 0, j = 0; static synchronized void one() { i++; j++; } static synchronized void two() { System.out.println("i=" + i + " j=" + j); } }This prevents methodone
and methodtwo
from being executed concurrently, and furthermore guarantees that the shared values ofi
andj
are both updated before methodone
returns. Therefore methodtwo
never observes a value forj
greater than that fori
; indeed, it always observes the same value fori
andj
.Another approach would be to declarei
andj
to bevolatile
:class Test { static volatile int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } }This allows methodone
and methodtwo
to be executed concurrently, but guarantees that accesses to the shared values fori
andj
occur exactly as many times, and in exactly the same order, as they appear to occur during execution of the program text by each thread. Therefore, the shared value forj
is never greater than that fori
, because each update toi
must be reflected in the shared value fori
before the update toj
occurs. It is possible, however, that any given invocation of methodtwo
might observe a value forj
that is much greater than the value observed fori
, because methodone
might be executed many times between the moment when methodtwo
fetches the value ofi
and the moment when method two fetches the value ofj
.See §17.4 for more discussion and examples.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.