An example for deadlock : Synchronize « Thread « SCJP

class MainClass implements Runnable {
  Object firstResource;

  Object secondResource;

  public MainClass(Object first, Object second) {
    firstResource = first;
    secondResource = second;

  public void run() {
    while (true) {
      synchronized (firstResource) {
        synchronized (secondResource) {
          try {
          } catch (InterruptedException ex) {

  public static void main(String[] args) {
    Object a = "Resource A";
    Object b = "Resource B";
    Thread t1 = new Thread(new MainClass(a, b));
    Thread t2 = new Thread(new MainClass(b, a));

7.9.1.synchronized code
7.9.2.Synchronize an entire method by putting the synchronized modifier in the method's declaration.
7.9.3.Synchronizing Part of a Method
7.9.4.An example for deadlock
7.9.5.To synchronize part of a method preceded by synchronized(this).
7.9.6.When used with a code block, synchronized must be applied to an object.