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.
volatileFieldsThe 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,volatilefields, 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.volatileFieldsIf, in the following example, one thread repeatedly calls the methodone(but no more thanInteger.MAX_VALUEtimes 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 methodtwocould occasionally print a value forjthat is greater than the value ofi, because the example includes no synchronization and, under the rules explained in §17.4, the shared values ofiandjmight be updated out of order.One way to prevent this out-or-order behavior would be to declare methodsoneandtwoto 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 methodoneand methodtwofrom being executed concurrently, and furthermore guarantees that the shared values ofiandjare both updated before methodonereturns. Therefore methodtwonever observes a value forjgreater than that fori; indeed, it always observes the same value foriandj.Another approach would be to declareiandjto 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 methodoneand methodtwoto be executed concurrently, but guarantees that accesses to the shared values foriandjoccur 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 forjis never greater than that fori, because each update toimust be reflected in the shared value foribefore the update tojoccurs. It is possible, however, that any given invocation of methodtwomight observe a value forjthat is much greater than the value observed fori, because methodonemight be executed many times between the moment when methodtwofetches the value ofiand 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.