Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

What does the transient and volatile keywords do?

Thread view: 
res7cxbi@verizon.net - 21 Jan 2006 05:06 GMT
What does the transient and volatile keywords do? Why would i use them?

I heard that the volatile keyword has not been fully implemented in
Sun's JVM. But yet most of the JDK (especially the I/O classes) use
this keyword. What's up with that?
IchBin - 21 Jan 2006 05:24 GMT
> What does the transient and volatile keywords do? Why would i use them?
>
> I heard that the volatile keyword has not been fully implemented in
> Sun's JVM. But yet most of the JDK (especially the I/O classes) use
> this keyword. What's up with that?

When your not sure consult the 'Bible', 'Java™ Language Specification'
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#78119

8.3.1.3 transient Fields
Variables may be marked transient to indicate that they are not part of
the persistent state of an object.

If an instance of the class Point:

    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 specify
details of such services; see the specification of java.io.Serializable
for an example of such a service.

8.3.1.4 volatile Fields
As described in §17, the Java programming language allows threads to
access shared variables. 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 declared volatile, in which case the Java memory model
(§17) ensures that all threads see a consistent value for the variable.

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.

Signature

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

res7cxbi@verizon.net - 21 Jan 2006 07:26 GMT
Roedy Green - 21 Jan 2006 09:20 GMT
>What does the transient and volatile keywords do? Why would i use them?

http://mindprod.com/jgloss/transient.html
http://mindprod.com/jgloss/serialisation.html
http://mindprod.com/jgloss/volatile.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.