Avatar billede roo104 Nybegynder
28. maj 2004 - 16:50 Der er 7 kommentarer og
1 løsning

volatile variable

Hviken betydning ahr det hvis men erklærer en variabel volatile ?
Avatar billede riversen Nybegynder
28. maj 2004 - 16:54 #1
hvis du serialiserer objektet og skriver det til en fil kommer variablen ikke med
Avatar billede riversen Nybegynder
28. maj 2004 - 16:55 #2
sorry, det er transient :-)
Avatar billede arne_v Ekspert
28. maj 2004 - 17:02 #3
8.3.1.4 volatile Fields
As described in §17, the Java programming language allows threads that access shared variables to keep private working copies of the variables; this allows a more efficient implementation 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.
The Java programming language provides a second mechanism, volatile fields, that is more convenient for some purposes.

A field may be declared volatile, in which case a thread must reconcile its working 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 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.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 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 guarantees 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 program text by each thread. Therefore, the shared value for j is never 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.
Avatar billede arne_v Ekspert
28. maj 2004 - 17:03 #4
Uddrag fra JLS (Java Language Specification)

Jeg mener stort set ikke at volatile bruges i Java.
Avatar billede roo104 Nybegynder
28. maj 2004 - 17:07 #5
ok .. fandt det i en gammel bog om tråde. det vil sige at hvis flere tråde kan manipulere med en variabel og man vil sikre hvis en tråd har ændret den, afspejler ændringerne sig også i de andre tråde som har adgang til variablen, eller ?
Avatar billede arne_v Ekspert
28. maj 2004 - 17:18 #6
ja sådan cirka

man kunne også formulere det som: lad være med at optimere på denne variabel
Avatar billede roo104 Nybegynder
28. maj 2004 - 17:40 #7
smid et svar arne
Avatar billede arne_v Ekspert
28. maj 2004 - 18:26 #8
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester