Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Threads - synchronized kewyord

  • threads often need to share a common resource ie a file, with one thread reading from the file while another thread writes to the file
  • this is an example of a producer/consumer relationship

Race conditions

  • race conditions occur when multiple, asynchronously executing threads access the same object returning unexpected (wrong) results
  • they can be avoided by synchronizing the methods which access the shared resource
  • the Sun Thread tutorial has an example which uses a Producer class, and a Consumer class which respectively write and read integers from a CubbyHole class. If the CubbyHole class is unsynchronized, as in the following code:
public class CubbyHole {
    private int contents;

    public int get() {
        return contents;
    }

    public synchronized void put(int value) {
        contents = value;
    }
}

Example output from an unsynchronized Producer/Consumer

Consumer #1 got: 0
Producer #1 put: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Consumer #1 got: 0
Producer #1 put: 1
Producer #1 put: 2
Producer #1 put: 3
Producer #1 put: 4
Producer #1 put: 5
Producer #1 put: 6
Producer #1 put: 7
Producer #1 put: 8
Producer #1 put: 9
  • results are unpredictable; a number may be read before a number has been produced or multiple numbers may be produced with only one or two being read
  • adding synchronization ensures that a number is first produced, then read in the correct order
public class CubbyHole {
    private int contents;
    private boolean available = false;

    public synchronized int get() {
        while (available == false) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        available = false;
        notifyAll();
        return contents;
    }

    public synchronized void put(int value) {
        while (available == true) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        contents = value;
        available = true;
        notifyAll();
    }
}
  • the keyword synchronized is added to the method declarations
  • the Object methods wait() and notifyAll() are used to communicate between executing threads

Output after code is synchronized

Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9

Source for synchronized version

Other examples using synchronized

  • An example of using synchronized methods and object locks Thread3
  • An example using a synchronized statement on a common object Thread4
  • An example of synchronizing access to variables Account
  • An example of a museum which uses Walkmen radios for tours: WalkmanHire uses Museum, Counter, and Visitors classes.
Overview Thread Class Runnable Interface Thread States Scheduling Ending a Thread
  Execution Synchronization Locking Protocols synchronized keyword wait() notify(), notifyAll()
  Thread Mechanics