8.3.2
Initialization of Fields
CLASSES
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 method
one
 and method
two
 from being executed concurrently, and
furthermore guarantees that the shared values of
i
 and
j
 are both updated before
method
one
 returns. Therefore method
two
 never observes a value for
j
 greater
than that for
i
; indeed, it always observes the same value for
i
 and
j
.
Another approach would be to declare
i
 and
j
 to be
volatile
:
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 method
one
 and method
two
 to be executed concurrently, but guaran 
tees that accesses to the shared values for
i
 and
j
 occur exactly as many times,
and in exactly the same order, as they appear to occur during execution of the pro 
gram text by each thread. Therefore, method
two
 never observes a value for
j
greater than that for
i
, because each update to
i
 must be reflected in the shared
value for
i
 before the update to
j
 occurs. It is possible, however, that any given
invocation of method
two
 might observe a value for
j
 that is much greater than the
value observed for
i
, because method
one
 might be executed many times between
the moment when method
two
 fetches the value of
i
 and the moment when
method
two
 fetches the value of
j
.
See  17 for more discussion and examples.
A compile time error occurs if a
final
 variable is also declared
volatile
.
8.3.2   Initialization of Fields
If a field declarator contains a
variable initializer
, then it has the semantics of an
assignment ( 15.25) to the declared variable, and:
148






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