Results 1 to 2 of 2
  1. #1
    jalligator is offline Member
    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0

    Default what is wrong with volatile variables?

    Guys,

    What is wrong with snippet below? Why both conditions sometimes are true at the same time:
    if ( fine != suspect ) {
    if ( fine == suspect ) {
    Why foobar is printed out? I had a long time arguments, but failed to prove anything. Could you help me please? What do you think?

    Java Code:
    public class VWC {
      private volatile boolean  suspect;
      private boolean            fine;
      private boolean            anotherFine;
      private Object             lock = new Object();
      private int                   failures;
      private int                   iterations;
     
      private VWC() {
        new Thread() {
          public void run() {
            while( true ) {
              synchronized( lock ) {
                try {
                  lock.wait();
                } catch ( Exception exception ) {
                }
              }
              synchronized( lock ) {
                if ( fine != suspect ) {
                  System.err.println( "foo" );
                  if ( fine == suspect ) {
                    System.err.println( "bar" );
                  }
                }
                if ( fine != anotherFine ) {
                  System.err.println( "baz" );
                }
              }
            }
          }
        }.start();
      }
     
      private void run() {
        while( true ) {
          synchronized( lock ) {
            ++iterations;
            fine = ! fine;
            anotherFine = ! anotherFine;
          }
          suspect = ! suspect;
          synchronized( lock ) {
            lock.notify();
          }
        }
      }
     
      public static void main(String[] args) {
        new VWC().run();
      }
    }

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    How about cleaning up your code some.

    Java Code:
                if ( fine != suspect ) {
                  System.err.println( "foo" );
                  if ( fine == suspect ) {
                    System.err.println( "bar" );
                  }
                }
    should really be written clearly, which means make the test obvious.

    Java Code:
     
               if ( fine == suspect ) {
                    System.err.println( "is same" );
               else {
                    System.err.println( "not same at all" );
               }
    Your code is not obvious, and non-obvious code can hide many bugs

Similar Threads

  1. Keyword : volatile
    By peiceonly in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 12:47 PM
  2. wrong values
    By mark-mlt in forum New To Java
    Replies: 8
    Last Post: 04-25-2008, 11:11 AM
  3. volatile and synchornized
    By goldhouse in forum Threads and Synchronization
    Replies: 3
    Last Post: 04-23-2008, 08:38 PM
  4. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:16 PM
  5. volatile variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •