HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Recognizing and Eliminating Errors in Multithreaded Java"
Vol. 6, Issue 9, p. 40

	


Listing 1

class Deadlocker
{
  int field_1;       private Object lock_1 = new int[1];
  int field_2;        private Object lock_2 = new int[1];
  public void method1( int value )
     {       synchronized( lock_1 )
      {       synchronized( lock_2 )
        {
          field_1 = 0;
          field_2 = 0;
        }
      }
    }
  public void method2( int value )
    {       synchronized( lock_2 )
      {       synchronized( lock_1 )
        {
          field_1 = 0;
          field_2 = 0;
        }
      }
    }
}




Listing 2

 public class queue {
  static java.lang.Object queueLock_;
  Producer                    producer_;
  Consumer                    consumer_;


  public class Producer
  {
       void produce()
       {
           while (! done) {
             synchronized (queueLock_) {
                  produceItemAndAddItToQueue();
                  synchronized (consumer_) {
                          consumer_.notify();


                  }
             }
           }
  }


    public class Consumer
     {
       consume()
       {
           while (! done) {
                      synchronized (queueLock_) {
                           synchronized (consumer_) {
                                consumer_.wait();
           }
                            removeItemFromQueueAndProcessIt();
                      }
                 }
        }
    }
 }



Listing 3

 public class House {
  public volatile boolean foundationReady_ = false;
                   }


public class FoundationPourer extends Thread {
  public void run() {
    House a = getHouse();


// lay the foundation...


    a.foundationReady_ = true;
                }
}


public class BrickLayer extends Thread {
  public void run() {
    House a = getHouse();


//Wait until the foundation is ready
//NB:  This is a "busy wait", and is the *WRONG* way to
//do this; we should use wait() and
//Object.notify() instead.


      while (!a.foundationReady_) {
                          try {
                            Thread.sleep(500);
                              }
                          catch (Exception e) {
                            System.err.println
                              ("Caught exception:  "+e);
                                              }
                                  }
                    }
}




Listing 4

 public class Account {
  private int balance_;   // amount of money in the account, in cents


  public int getBalance(void) {
    return balance_;
                              }
  public void setBalance(int setting) {
    balance_ = setting;
                                      }
}


public class CustomerInfo {
  private int   numAccounts_;
  private Account[] accounts_;


  public void withdraw(int accountNumber, int amount)
          {
            int temp = accounts_[accountNumber].getBalance();
            temp = temp - amount;
            accounts_[accountNumber].setBalance(temp);
          }


  public void deposit(int accountNumber, int amount)
          {
            int temp = accounts_[accountNumber].getBalance();
            temp = temp + amount;
            accounts_[accountNumber].setBalance(temp);
          }
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.