2 Replies Latest reply: Mar 30, 2008 9:26 AM by 807591 RSS

    Problem with memory consistency errors in two thread

    807591
      hi guys:
      i read the java tutorial about Memory Consistency Errors. it mention that synchronization can create happens-before relationship to avoid memory consistency errors.
      and i run my own code. but the errors still there. the Thread b sometime will print out "0", if u run the program many times. Could any one explain this for me. Or is there any wrong with my code?
      public class Main 
      {
      
       
          public static void main(String[] args) 
          {
             final Counter counter = new Counter();
              Thread a = new Thread(new Runnable(){
                  public void run()
                  {
                     
                      counter.increment();
                     
                  }
              });
               
              Thread b = new Thread(new Runnable(){
                 public void run()
                 {
                   
                     System.out.println("b of result:"+counter.value());
                 }
              });
             
              
              a.start();
              b.start();
             
              
          }
      
      }
      
      class Counter 
      {
          private int c = 0;
      
          public synchronized void increment() {
              c++;
          }
      
          public synchronized void decrement() {
              c--;
          }
      
          public synchronized int value() {
              return c;
          }
      
      }
      thx
        • 1. Re: Problem with memory consistency errors in two thread
          807591
          JAVA-rookie wrote:
          hi guys:
          i read the java tutorial about Memory Consistency Errors. it mention that synchronization can create happens-before relationship to avoid memory consistency errors.
          and i run my own code. but the errors still there. the Thread b sometime will print out "0", if u run the program many times. Could any one explain this for me.
          The operating system starts thread A, then a context switch happens, putting A into a paused state. The OS starts thread B and gets as far as the println() statement before the next context switch back to Thread A.

          Jim S.
          • 2. Re: Problem with memory consistency errors in two thread
            807591
            It depends what you want to happen, but if you care about the order in which the counter is incremented and printed, then why put the two actions on separate threads?

            You can make one thread wait for another to reach a certain point, easilly enough.
            Object monitor = new Object();
            boolean canGoOn = false;
            
            private void  pause() throws InterruptedException {
                synchronized(monitor) {
                     while(!canGoOn)
                        monitor.wait();
                   }
               }
            
            private void release()  {
                   sychronized(monitor) {
                     canGoOn = true;
                     monitor.notifyAll();
                  }
              }
            The way you're usiing synchronized has little effect because the synchronization only lasts during the method call, not between them.