Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6470111

Deadlock in Exchanger

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP Home Edition Ver 2002 Service Pack 2

      A DESCRIPTION OF THE PROBLEM :
      java.util.concurrent.exchanger . If many processes wants to exchange, that will occur deadlock.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.concurrent.*;
      import java.util.*;
      public class ExecutorsAndBarriers2 {

          /**
           * @param args
           */
          public static void main(String[] args) {
              MyExecutor myExecutor = new MyExecutor();
              for(int i1=0;i1<1024;i1++)
                  myExecutor.execute(new MyThreadExchanger());
              new Thread(){
                  MyThreadExchanger m = new MyThreadExchanger();
                  public void run(){
                      while(true)
                      {
                          try
                          {
                              System.out.println("L: " + m.getLiving());
                              System.out.println("E: " + m.getInExchange());
                              Thread.sleep(4000);
                          }
                          catch(InterruptedException e){System.out.println(e.getLocalizedMessage());}
                      }
                  }
              }.start();
          }

      }


      class MyExecutor implements Executor{
          private static ExecutorService exs = Executors.newFixedThreadPool(3);
          public void execute(Runnable r)
          {
              exs.submit(r);
          }
      }

      class ObjectToExchange
      {
          private static int ID=0;
          private int myID;
          public ObjectToExchange(){
              myID=ID++;
          }
          public int getID(){
              return myID;
          }
      }

      class MyThreadExchanger implements Runnable{
          private static int ID=0;
          private static volatile int living=0;
          private int myID;
          private static volatile int inExchange=0;
          private static Exchanger<ObjectToExchange> exchanger = new Exchanger<ObjectToExchange>();
          private ObjectToExchange ob;
          public int getLiving()
          {
              return living;
          }
          public int getInExchange()
          {
              return inExchange;
          }
          public MyThreadExchanger(){
              myID=ID++;
              living++;
              ob=new ObjectToExchange();
          }
          public void run(){
              System.out.println(myID + " : START");
              try
              {
                  ObjectToExchange tmp;
                  inExchange++;
                  tmp = exchanger.exchange(ob);/*Implementacja z java z deadlock-iem*/
                  inExchange--;
                  System.out.println("Watek : " + myID + " : wymienil " + ob.getID() + " na : " + tmp.getID());
                  ob=tmp;
              }
              catch(InterruptedException ie){System.out.println(ie.getMessage());}
              System.out.println(myID + " : STOP " + --living);
          }
      }
      ---------- END SOURCE ----------

            martin Martin Buchholz
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: