STIW3054-A182 / Main-Issues

5 stars 1 forks source link

Deadlock #10

Open zhamri opened 5 years ago

zhamri commented 5 years ago

Instruction:

Below is the Deadlock class to execute TWO (2) threads.

public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }
}

Write the C2 and C2 classes to demonstrate the deadlock situation where both threads are blocked forever using synchronized keyword. The example of the output is shown below.

Example of output:

Thread 1: Holding lock 1... Thread 2: Holding lock 2... Thread 1: Waiting for lock 2... Thread 2: Waiting for lock 1...

SINHUI commented 5 years ago

Deadlock

public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

     public static void main(String args[]) {
            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
    }

C1

public class C1 extends Thread {

    @Override
    public void run() {
        C1 t1 = new C1();
        t1.setName("Thread 1");
        synchronized (Deadlock.Lock1) {
            System.out.println(t1.getName()+": Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println(t1.getName()+": Waiting for lock 2..."); 
    }
}

C2

public class C2 extends Thread {

    @Override
    public void run() {

        C2 t2 = new C2();
        t2.setName("Thread 2");
        synchronized (Deadlock.Lock1) {
            System.out.println(t2.getName()+": Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println(t2.getName()+": Waiting for lock 1...");     
    }
}

Output

output

shyyahcheang commented 5 years ago
package deadLock;

public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }

    private static class C1 extends Thread {
        public void run() {
            synchronized (Lock1) {
                System.out.println("Thread 1: Holding lock 1...");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 1: Waiting for lock 2...");
            }
        }
    }

    private static class C2 extends Thread {
        public void run() {
            synchronized (Lock2) {
                System.out.println("Thread 2: Holding lock 2...");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 2: Waiting for lock 1...");
            }
        }
    }
}

Output

issue9 - output deadlock

limxinyii commented 5 years ago

Deadlock

public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

     public static void main(String args[]) {
            C1 T1 = new C1(Lock1);
            C2 T2 = new C2(Lock2);
            T1.start();
            T2.start();
        }
    }

C1

public class C1 extends Thread {
    private Object lock1;

    public C1(Object k1) {
        lock1 = k1;
    }

    public void run() {
        synchronized (lock1) {
            System.out.println("Thread 1: Holding lock 1...");

            try {
            Thread.sleep(10);
            }catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println("Thread 1: Waiting for lock 1...");
        }

    }
}

C2

public class C2 extends Thread {
    private Object lock2;

    public C2(Object k2) {
        lock2 = k2;
    }

    public void run() {
        synchronized (lock2) {
            System.out.println("Thread 2: Holding lock 2...");

            try {
                Thread.sleep(10);
            }catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println("Thread 2: Waiting for lock 1...");
        }
    }

}

output

image

mimiothman commented 5 years ago

public class deadlock {
       public static Object Lock1 = new Object();
       public static Object Lock2 = new Object();

       public static void main(String args[]) {
          ThreadDemo1 T1 = new ThreadDemo1();
          ThreadDemo2 T2 = new ThreadDemo2();
          T1.start();
          T2.start();
       }

       private static class ThreadDemo1 extends Thread {
          public void run() {
             synchronized (Lock1) {
                System.out.println("Thread 1: Holding lock 1...");

                try { Thread.sleep(10); }
                catch (InterruptedException e) {}
                System.out.println("Thread 1: Waiting for lock 2...");

                synchronized (Lock2) {
                   System.out.println("Thread 1: Holding lock 1 & 2...");
                }
             }
          }
       }
       private static class ThreadDemo2 extends Thread {
          public void run() {
             synchronized (Lock2) {
                System.out.println("Thread 2: Holding lock 2...");

                try { Thread.sleep(10); }
                catch (InterruptedException e) {}
                System.out.println("Thread 2: Waiting for lock 1...");

                synchronized (Lock1) {
                   System.out.println("Thread 2: Holding lock 1 & 2...");
                }
             }
          }
       } 
    }

output image

roflinasuha commented 5 years ago
package deadlock;

public class Deadlock {
    // Creating Object Locks
    static Object Lock1 = new Object();
    static Object Lock2 = new Object();

    private static class ThreadName1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");
            /* Adding sleep() method so that
               Thread 2 can lock ObjectLock2 */
            try { 
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 1: Waiting for lock 2...");
            /*Thread 1 has ObjectLock1 
              but waiting for ObjectLock2*/
            synchronized (Lock2) {
               System.out.println("Thread 1: No DeadLock");
            }
         }
      }
   }
   private static class ThreadName2 extends Thread {
      public void run() {
         synchronized (Lock2) {
            System.out.println("Thread 2: Holding lock 2...");
            /* Adding sleep() method so that
               Thread 1 can lock ObjectLock1 */
            try { 
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 2: Waiting for lock 1...");
            /*Thread 2 has ObjectLock2 
              but waiting for ObjectLock1*/
            synchronized (Lock1) {
               System.out.println("Thread 2: No DeadLock");
            }
         }
      }
   }

   public static void main(String args[]) {
      ThreadName1 thread1 = new ThreadName1();
      ThreadName2 thread2 = new ThreadName2();
      thread1.start();
      thread2.start();
   }
}

image

lishaobin commented 5 years ago

deadlock

package issure10;

public class deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

     public static void main(String args[]) {
            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
    }

c1

package issure10;

public class C1  extends Thread {

    @Override
    public void run() {
        C1 t1 = new C1();
        t1.setName("Thread 1");
        synchronized (deadlock.Lock1) {
            System.out.println(t1.getName()+": Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println(t1.getName()+": Waiting for lock 2..."); 
    }

}

c2

package issure10;

public class C2  extends Thread{

    @Override
    public void run() {

        C2 t2 = new C2();
        t2.setName("Thread 2");
        synchronized (deadlock.Lock1) {
            System.out.println(t2.getName()+": Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println(t2.getName()+": Waiting for lock 1...");     
    }

}

image

lancelot9927 commented 5 years ago

DeadLock


public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

     public static void main(String args[]) {
            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
    }

C1

public class C1 extends Thread {

    @Override
    public void run() {

            synchronized (Deadlock.Lock1) {
            System.out.println("Thread 1: Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println("Thread 1: Waiting for lock 2...");  
    }
}

C2

public class C2 extends Thread {

    @Override
    public void run() {

        synchronized (Deadlock.Lock2) {
            System.out.println("Thread 2: Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            System.out.println("Thread 2: Waiting for lock 1...");      
    }
}

Screenshot

15

ChongMeiYong commented 5 years ago

Deadlock

public class Deadlock {

    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }

}

C1


public class C1 extends Thread{

    @Override
    public void run() {
        synchronized(Deadlock.Lock1){
            System.out.println("Thread 1: Holding lock 1...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 1: Waiting for lock 2...");
    }
}

C2

public class C2 extends Thread{

    @Override
    public void run() {
        synchronized(Deadlock.Lock1){
            System.out.println("Thread 2: Holding lock 2...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 2: Waiting for lock 1...");
    }
}

Output image

tanyujia commented 5 years ago

C1

public class C1 extends Thread {

    @Override
    public void run() {

        setName("Thread 1");

        synchronized (Deadlock.Lock1) {
            try {
                System.out.println(getName() + ": Holding lock 1...");
                sleep(10);
                System.out.println(getName() + ": Waiting for lock 2...");

            } catch (Exception ex) {

            }
        }
    }
}

C2


public class C2 extends Thread {

    @Override
    public void run() {

        setName("Thread 2");

        synchronized (Deadlock.Lock2) {
            try {
                System.out.println(getName() + ": Holding lock 2...");
                sleep(10);
                System.out.println(getName() + ": Waiting for lock 1...");

            } catch (Exception ex) {

            }
        }
    }
}

Output

(Issues 10) Snipaste_2019-04-08_01-57-30

TanShiJet commented 5 years ago

Code

public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }

    private static class C1 extends Thread {
          public void run() {
             synchronized (Lock1) {
                System.out.println("Thread 1: Holding lock 1...");

                try { Thread.sleep(0); }
                catch (InterruptedException e) {}
                System.out.println("Thread 1: Waiting for lock 2...");

                synchronized (Lock2) {
                   System.out.println("Thread 1: Holding lock 1 & 2...");
                }
             }
          }
       }
       private static class C2 extends Thread {
          public void run() {
             synchronized (Lock2) {
                System.out.println("Thread 2: Holding lock 2...");

                try { Thread.sleep(0); }
                catch (InterruptedException e) {}
                System.out.println("Thread 2: Waiting for lock 1...");

                synchronized (Lock1) {
                   System.out.println("Thread 2: Holding lock 1 & 2...");
                }
             }
          }
       } 
    }

Output

image

Yvevonwen commented 5 years ago

JavaCode

package issue10;
public class DeadLock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        C1 t1 = new C1();
        C2 t2 = new C2();
        t1.start();
        t2.start();
    }
}
package issue10;

public class C1 extends Thread{
    public void run() {
        synchronized(DeadLock.Lock1){
            System.out.println("Thread 1: Holding lock 1...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 1: Waiting for lock 2...");
    }
}
package issue10;

public class C2 extends Thread{
    @Override
    public void run() {
        synchronized(DeadLock.Lock1){
            System.out.println("Thread 2: Holding lock 2...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 2: Waiting for lock 1...");
    }
}

Output

image

raihanwidia commented 5 years ago
package issues10;

public class Deadlock {

        public static Object Lock1 = new Object();
        public static Object Lock2 = new Object();

        public static void main(String args[]) {
            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
    }
package issues10;

public class C1 extends Thread {

    public void run() {

    synchronized (Deadlock.Lock1) {
        System.out.println("Thread 1: Holding lock 1...");
        try { Thread.sleep(100); }
        catch (InterruptedException e) {}
        System.out.println("Thread 1: Waiting for lock 2...");
        synchronized (Deadlock.Lock2) {
            System.out.println("Acquired lock on Integer.class object");
        }
    }
    }

}
package issues10;

public class C2 extends Thread {

    public void run() {
    synchronized (Deadlock.Lock2) {
        System.out.println("Thread 2: Holding lock 2...");
        try { Thread.sleep(100); }
        catch (InterruptedException e) {}
        System.out.println("Thread 2: Waiting for lock 1...");
        synchronized (Deadlock.Lock1) {
            System.out.println("Acquired lock on Integer.class object");
        }
    }
    }
}

image

lingzhongli commented 5 years ago
package deadlock;

public class Deadlock {

    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String[] args) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }

}
package deadlock;

public class C1 extends Thread {

            @Override
        public void run() {

        synchronized (Deadlock.Lock1) {
            System.out.println("Thread 1 : Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (Deadlock.Lock2) {
                         System.out.println("Thread 1: Waiting for lock 2...");}
    }

}
package deadlock;

public class C2 extends Thread {

            public void run() {

        synchronized (Deadlock.Lock2) {
            System.out.println("Thread 2 : Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (Deadlock.Lock1) {
                         System.out.println("Thread 2: Waiting for lock 1...");}
    }

}

image

prevenkumar commented 5 years ago
public class Deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }
}

public class C1 extends Thread{

    public void run() {
        synchronized(Deadlock.Lock1){
            System.out.println("Thread 1: Holding lock 1...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 1: Waiting for lock 2...");
    }
}

public class C2 extends Thread {

    public void run() {
        synchronized(Deadlock.Lock1){
            System.out.println("Thread 2: Holding lock 2...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 2: Waiting for lock 1...");
    }
}

issues10

HENGZHIYONG commented 5 years ago

Deadlock


package Deadlock;

public class Deadlock {

    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String[] args) {

            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
    }

C1

package Deadlock;

public class C1 extends Thread{
    @Override
    public void run() {
        synchronized(Deadlock.Lock1){
            System.out.println("Thread 1: Holding lock 1...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 1: Waiting for lock 2...");
    }
}

C2

package Deadlock;

public class C2 extends Thread{
    @Override
    public void run() {
    synchronized(Deadlock.Lock2){
        System.out.println("Thread 2: Holding lock 2 ...");
    }
    try {
        Thread.sleep(10);
    }catch(InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Thread 2: Waiting for lock 1...");
}
}

Output ` image

WongKinSin13 commented 5 years ago

DeadlockTest.java

public class DeadlockTest {
        public static Object Lock1 = new Object();
        public static Object Lock2 = new Object();

        public static void main(String args[]) {
            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
}

C1.java

public class C1 extends Thread{

    public void run() {
        C1 t1 = new C1();
        t1.setName("Thread 1");
        synchronized(DeadlockTest.Lock1) {
            System.out.println(t1.getName()+ ": Holding lock 1...");
        }
        try {
            t1.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getName()+ ": Waiting lock 1...");
    }
}

C2.java

public class C2 extends Thread{

    public void run() {
        C2 t2 = new C2();
        t2.setName("Thread 2");
        synchronized(DeadlockTest.Lock2) {
            System.out.println(t2.getName()+ ": Holding lock 2...");
        }
        try {
            t2.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t2.getName()+ ": Waiting lock 2...");
    }
}

Output

image

FatihahFauzi commented 5 years ago

class deadlock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }
}

class C1 extends Thread{

    public void run() {
        synchronized(deadlock.Lock1){
            System.out.println("Thread 1: Holding lock 1...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 1: Waiting for lock 2...");
    }
}

class C2 extends Thread {

    public void run() {
        synchronized(deadlock.Lock1){
            System.out.println("Thread 2: Holding lock 2...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 2: Waiting for lock 1...");
    }
}

Capture10

sonyhana7 commented 5 years ago

MAIN CLASS

public class DeadLocks {

        public static Object Lock1 = new Object();
        public static Object Lock2 = new Object();

        public static void main(String args[]) {
            C1 T1 = new C1();
            C2 T2 = new C2();
            T1.start();
            T2.start();
        }
    }

C1 CLASS

public class C1 extends  Thread{

        public void run() {
            synchronized(DeadLocks.Lock1){
                System.out.println("Thread 1: Holding lock 1...");
            }
            try {
                Thread.sleep(10);
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 1: Waiting for lock 2...");
        }
    }

C2 CLASS


public class C2 extends  Thread {

    public void run() {
        synchronized(DeadLocks.Lock1){
            System.out.println("Thread 2: Holding lock 2...");
        }
        try {
            Thread.sleep(10);
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread 2: Waiting for lock 1...");
    }
}

image

muhammadbimo1 commented 5 years ago
public class C1 extends Thread {

    @Override
    public void run() {
        C1 t1 = new C1();
        t1.setName("Thread 1");
        synchronized (DeadlockTest.Lock1) {
            System.out.println(t1.getName()+": Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getName()+": Waiting for lock 2...");
    }
}
public class C2 extends Thread {

    @Override
    public void run() {

        C2 t2 = new C2();
        t2.setName("Thread 2");
        synchronized (DeadlockTest.Lock2) {
            System.out.println(t2.getName()+": Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t2.getName()+": Waiting for lock 1...");
    }
}
public class DeadlockTest {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }
}

output

"C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\lib\idea_rt.jar=62716:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\Users\beemo\Documents\NetBeansProjects\deadlockissue\out\production\deadlockissue" DeadlockTest
Thread 1: Holding lock 1....
Thread 2: Holding lock 2....
Thread 1: Waiting for lock 2...
Thread 2: Waiting for lock 1...
trinanda98 commented 5 years ago

Deadlock.java

package DeadlockIssue;

public class Deadlock {

    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }
}

C1.java

package DeadlockIssue;

public class C1 extends Thread{
      public void run() {  
          synchronized (Deadlock.Lock1) {  
           System.out.println("Thread 1: Holding lock 1...");  

           try {
               Thread.sleep(10);} 
           catch (InterruptedException  e) {}
               System.out.println("Thread 1: Waiting for lock 2...");

           synchronized (Deadlock.Lock2) {  

           }  
         }  
}
}

C2

package DeadlockIssue;

public class C2 extends Thread{
      public void run() {  
          synchronized (Deadlock.Lock2) {  
           System.out.println("Thread 2: Holding lock 2...");  

           try { 
               Thread.sleep(10);} 
           catch (InterruptedException  e) {}
               System.out.println("Thread 2: Waiting for lock 1..."); 

           synchronized (Deadlock.Lock1) {  

           }  
         }  
}
}
Screenshot (81)
rahimahsahar commented 5 years ago
package deadlock;

public class Deadlock {

        // Create Object Locks
        static Object Lock1 = new Object();
        static Object Lock2 = new Object();

        private static class Thread1 extends Thread {
          public void run() {
             synchronized (Lock1) {
                System.out.println("Thread 1: Holding lock 1...");
                /* Add sleep() method so that
                   Thread 2 can lock ObjectLock2 */
                try { 
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 1: Waiting for lock 2...");
                /*Add Thread 1 has ObjectLock1 
                  but waiting for ObjectLock2*/
                synchronized (Lock2) {
                   System.out.println("Thread 1: No DeadLock");
                }
             }
          }
       }
       private static class Thread2 extends Thread {
          public void run() {
             synchronized (Lock2) {
                System.out.println("Thread 2: Holding lock 2...");
                /* Add sleep() method so that
                   Thread 1 can lock ObjectLock1 */
                try { 
                    Thread.sleep(100);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 2: Waiting for lock 1...");
                /*Thread 2 has ObjectLock2 
                  but waiting for ObjectLock1*/
                synchronized (Lock1) {
                   System.out.println("Thread 2: No DeadLock");
                }
             }
          }
       }

       public static void main(String args[]) {
          Thread1 thread1 = new Thread1();
          Thread2 thread2 = new Thread2();
          thread1.start();
          thread2.start();
       }
    }

issues10

alfmrza commented 5 years ago

MAIN

package COM.STIW3054.Test;

public class issue10 { // DEADLOCK
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        issue10C1 T1 = new issue10C1();
        issue10C2 T2 = new issue10C2();
        T1.start();
        T2.start();
    }
}

C1

package COM.STIW3054.Test;

public class issue10C1 extends Thread {

    @Override
    public void run() {
        issue10C1 t1 = new issue10C1();
        t1.setName("T1");
        synchronized (issue10.Lock1) {
            System.out.println(t1.getName() + ": Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getName() + ": Waiting for lock 2...");
    }
}

C2

package COM.STIW3054.Test;

public class issue10C2 extends Thread {

    @Override
    public void run() {

        issue10C2 t2 = new issue10C2();
        t2.setName("T1");
        synchronized (issue10.Lock1) {
            System.out.println(t2.getName() + ": Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t2.getName() + ": Waiting for lock 1...");
    }
}

image

FatihahFauzi commented 5 years ago
public class DeadLock {
    public static Object Lock1 = new Object();
    public static Object Lock2 = new Object();

    public static void main(String args[]) {
        C1 T1 = new C1();
        C2 T2 = new C2();
        T1.start();
        T2.start();
    }
}

 class C1 extends Thread {

    @Override
    public void run() {
        C1 t1 = new C1();
        t1.setName("Thread 1");
        synchronized (DeadLock.Lock1) {
            System.out.println(t1.getName()+": Holding lock 1....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getName()+": Waiting for lock 2...");
    }
}

class C2 extends Thread {

    @Override
    public void run() {

        C2 t2 = new C2();
        t2.setName("Thread 2");
        synchronized (DeadLock.Lock1) {
            System.out.println(t2.getName()+": Holding lock 2....");
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t2.getName()+": Waiting for lock 1...");
    }
}

output:
"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3\lib\idea_rt.jar=63285:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\User\IdeaProjects\DeadLock\out\production\DeadLock DeadLock
Thread 1: Holding lock 1....
Thread 2: Holding lock 2....
Thread 1: Waiting for lock 2...
Thread 2: Waiting for lock 1...

Process finished with exit code 0