Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

how do i make deadlock

I  read the following texts to create a deadlock.

in general, deadlock occurs when two or more threads each obtain two or more locks, but don't obtain them in the same order:

// T1
synchronized (a) {
    synchronized (b) {
    }
}
 
 
// T2
synchronized (b) {
    synchrnoized(a) {
    }
}





Now, i want to create a deadlock which conforms the above.

but i am confused how to write..

few issues i am stuck..
1)whether T1 and T2 are generated  from the same class .
2)whether a,b are the objects of the same shared resource.
3)how do i write the above sychronized methods ? see, Runnable class has only 1 method run() for thread activities .....how do  i write there 4 synchronized statements in it ?


I will appreciate if you please  provide me a deadlock code which conforms the above  code snippet.
ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cofactor
cofactor

ASKER

> 2)whether a,b are the objects of the same shared resource.
must be

what i mean here ..is whther a could be an object of class of  CUSTOMER  and b could be an object of EMPLOYEE class.

but it seems , you are suggesting both the objects will be of the same class(say both of them are of CUSTOMER class) ......is it really necessary ? i dont think so .....just a guess.




AND about your code example :

 you are making 2 runnables here  runnable1 , runnable2 .

do you want to run this way ?

Thread T1=new Thread(runnable1);
T1.start();

Thread T2=new Thread(runnable2);
T2.start();


well ...ok .....BUT I was thinking that    there would be one  Runnable  with a single  run ()method  and  the 2  threads would  be created via a loop  and they will use the single runnable........is it too  imaginary ? can it  be codified ?  its my guess because author did not mention anything .

Thanks
can you please post the full code ? i would like to run an test  how the deadlock is working here .
> but it seems , you are suggesting both the objects will be of the same
> class(say both of them are of CUSTOMER class) ......is it really necessary ?
NO - the objects a and b should be same (no matter whether they belong to same class or not)

> there would be one  Runnable  with a single  run ()method  and  the 2  threads would  be created via a loop  and they will use the single runnable

It wouldn't create deadlock.
> can you please post the full code ?
It would be against EE policy.
>it wouldn't create deadlock.

so , from your comment..i can draw a conclusion that , if i  create  multiple threads  via loop  (say for loop  )  with a single runnable object with a lock to some object say ArrayList......i'll never get a deadlock.......right ?

provided these are ONLY threads on your jvm. If you have more, you need to figure out there the resources you are locking inside these threads are also accessible and synchronized by other threads.
>NO - the objects a and b should be same (no matter whether they belong to same class or not)

what do you mean by same ?

say in my thread class  i have this ..

MyThread implements Runnable
{
Customer a
Employee b
Thread t

// run method and blah blah

}



do you have objection here ?  see a and b are not same type .

do you mean this can not create deadlock ?
>provided these are ONLY threads on your jvm.
ok.

 >If you have more,..
I am not going to create more ...i want to create just 2 threads via coding......do you mean some default JVM threads .....well , but they are not coming to grab my a and b ....is not it ?

>you need to figure out there the resources you are locking inside these threads are also accessible and synchronized by other threads.
not understandable where from others threads coming and why they are fighting with my resouces a,b.
i meant a, b sould point to same instance. Take this example:

T1
Customer a = new Customer();
Employee b = new Employee();

synchronized(a) {
    synchronized(b) {
    }
}

T2
Customer a = new Customer();
Employee b = new Employee();

synchronized(b) {
    synchronized(a) {
    }
}

This will NEVER create deadlock, as variables a and b are not shared and point to different instances. Deadlock typically happens when the instances are shared to different threads and order of lock is NOT same.
class MyThread2 implements Runnable {
   String s1;
   String a="abc";
   String b="def";

   Thread t;
   public MyThread2 (String s2) {
     
        s1= s2;
        t = new Thread(this);
        t.start();
   }
   public void run() {
   synchronized (a) {
      synchronized (b) {
      }
   }
}
}



class MyThread1 implements Runnable {
   String s1;
   String a="abc";
   String b="def";
   Thread t;
   public MyThread1 (String s2) {
     
      s1= s2;
      t = new Thread(this);
      t.start();
   }
   public void run() {
   synchronized (b) {
      synchronized (a) {
      }
   }
}
}
class Demo{
   public static void main (String args[]) {
     
    System.out.println("DEADLOCK TEST");
   
     MyThread1 name1 = new MyThread1("Bob");
     MyThread2 name2 = new MyThread2("Mary");
     
      System.out.println("hello");
     
  }
}






THIS CODE IS NOT SHOWING DEADLOCK
i guess this is not working because a,b are not treated as shared resource ...... but how do make it a shared resource ?  even if i do that ...will i get the deadlock ?

how do i understand that a deadlock has been created ? does printout helps ?
wrap the synchronized block in while on both threads

while(true) {
synchronized (b) {
     synchronized (a) {
     }
}
}
>while(true)

but this is an infinite loop !!

once a thread enters into it ...it will never come out .


But we are not using the property  

" ....Deadlock typically happens when the instances are shared to different threads and order of lock is NOT same......"

iwhat u r suggesting is to put a while so that thread will  never come out ......if that was our intention...then  there is no  meaning of  using l synchronisation  and orders blah blah....is not it ? because while loop is all-in-all .

cant we do it without while loop ?  OR is it absolutely necessary to make a deadlock one MUST use a while loop ?



Here is my latest code :


class MyThread2 implements Runnable {
   String s1;
   String a="abc";
   String b="def";

   Thread t;
   public MyThread2 (String s2) {
     
        s1= s2;
        t = new Thread(this);
        t.start();
   }
   
 
   public void run() {
         
      while(true){
      
   synchronized (a) {
      synchronized (b) {
      }
   }
   }

}
}



class MyThread1 implements Runnable {
   String s1;
   String a="abc";
   String b="def";
   Thread t;
   public MyThread1 (String s2) {
     
      s1= s2;
      t = new Thread(this);
      t.start();
   }
   public void run() {
      while(true){
   synchronized (b) {
      synchronized (a) {
      }
   }
      }
}
}
class Demo{
   public static void main (String args[]) {
     
    System.out.println("DEADLOCK TEST");
   
     MyThread1 name1 = new MyThread1("Bob");
     MyThread2 name2 = new MyThread2("Mary");
     
      System.out.println("hello");
     
  }
}



here is my output:
DEADLOCK TEST
hello



is it a deadlock ?  how do i know its a deadlock ?
we need to make sure that, when thread T1 acquies lock on object a, T2 should also has acquired lock on object b. So wrapping around while will make sure that at some point of time that would happen
ok...i run the above code  but its giving output .

How do i understand my code has got a deadlock ?

though i see , the program does not ends up   ...but   that may be  because of while loop .....how can i make sure that the code has fallen into  a deadlock ?

is there any specific print order or some deterministic rule exists  to check whether a code has fallen into deadlock .....i wish to  check it out .

You can check as:

1. If you run on IDE, you can figureout that the threads are waiting for each other (by looking into monitors).
2. Since the program is while(true), if it is on NOT on deadlock, it would consume all the cpu, otherwise the cpu consumption would be very very less - as the threads a waiting for each other
>If you run on IDE, you can figureout that the threads are waiting for each other (by looking into monitors).

i am running it on  Websphere IDE .
is it possible to see in this IDE ?


>Since the program is while(true), if it is on NOT on deadlock, it would consume all the cpu, >otherwise the cpu consumption would be very very less - as the threads a waiting for each other

i see...my CPU consumption is less.
>m running it on  Websphere IDE
5.1.1 version
Hi cofactor,

You have to put a sleep statement between the two synchronized to get a deadlock :

   synchronized (a) {
     Thread.sleep(1000); // let time for the other thread to get b
     synchronized (b) {
     }
   }

and:

   synchronized (b) {
     Thread.sleep(1000); // let time for the other thread to get a
     synchronized (a) {
     }
   }
Also you don't need the while loop, and if you want to see effect :

   System.out.println("thread 1 start ...");
   synchronized (a) {
     Thread.sleep(1000); // let time for the other thread to get b
     synchronized (b) {
     }
   }
   System.out.println("thread 1 end."); // never displayed if deadlock

and:

   System.out.println("thread 2 start ...");
   synchronized (b) {
     Thread.sleep(1000); // let time for the other thread to get a
     synchronized (a) {
     }
   }
   System.out.println("thread 2 end."); // never displayed if deadlock
Not sure how to figure out on  Websphere IDE? may be running the program on debug mode and suspending it would show the threads.
one question here .

did you see i am using  

String a="abc";
String b="def";

as  the  same meber variable in 2 classes ........this not so called "shared variable"....right ? .......still  will i be able to see the deadlock effect ?
> as  the  same meber variable in 2 classes ........this not so called
> "shared variable"....right ? .......still  will i be able to see the
> deadlock effect ?
yes, there would be deadlock as the objects are shared (becase of String object initialized using quotes)
String a = "abc";
String b = "abc"; // a and b objects are same
String c = new String("abc"); // this is different
>there would be deadlock as the objects are shared

shared ?
how it has been shared ? they are different  .....they are defined  INDIVIDUALLY  in 2 classes i.e MyThread1  and MyThread2 ........can we call this shared ?  bit confused.....is really shared ?


what i understand by sharing is as follows

MyThreadClass implements Runnable
{

SharedObject  sho;   // SharedObject is a class defined in some other may be

//blah blah

}



Now , If many threads uses this  same class  MyThread class  to start.....then   sho would be shared object.


but in my above example , that seems i did not share  a, b across  2 threads ...is not it ?
so, i can not actually say..its a shared variable...and 2 threads are sharing this variable.

whats your opinion ?
> but in my above example , that seems i did not share  a, b across  2
> threads ...is not it ?
JVM makes it same object (as i said earlier) - this is valid only for String object.
but they are containg different values ? "abc"  and "def"



"abc" and "def" are different,
but

String x = "abc";
String y = "abc";
are same.
In fact, String constants are from the constant pool.
String x = "abc";
String y = "abc";

x and y are two references to the same entry in the constant pool.