CLASSES
Field Modifiers
8.3.1
class Point {
int x, y;
transient float rho, theta;
}
were saved to persistent storage by a system service, then only the fields
x
 and
y
would be saved. This specification does not yet specify details of such services;
we intend to provide them in a future version of this specification.
8.3.1.4
volatile
Fields
As described in  17, the Java language allows threads that access shared variables
to keep private working copies of the variables; this allows a more efficient imple 
mentation of multiple threads. These working copies need be reconciled with the
master copies in the shared main memory only at prescribed synchronization
points, namely when objects are locked or unlocked. 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.
Java provides a second mechanism that is more convenient for some purposes:
a field may be declared
volatile
, in which case a thread must reconcile its work 
ing copy of the field with the master copy every time it accesses the variable.
Moreover, operations on the master copies of one or more volatile variables on
behalf of a thread are performed by the main memory in exactly the order that the
thread requested.
If, in the following example, one thread repeatedly calls the method
one
 (but
no more than
Integer.MAX_VALUE
 ( 20.7.2) times in all), and another thread
repeatedly calls the method
two
:
class Test {
static int i = 0, j = 0;
static void one() { i++; j++; }
static void two() {
System.out.println("i=" + i + " j=" + j);
}
}
then method
two
 could occasionally print a value for
j
 that is greater than the
value of
i
, because the example includes no synchronization and, under the rules
explained in  17, the shared values of
i
 and
j
 might be updated out of order.
One way to prevent this out or order behavior would be to declare methods
one
 and
two
 to be
synchronized
 ( 8.4.3.5):
147






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved