Home  >  Article  >  Java  >  How can we avoid deadlock in Java?

How can we avoid deadlock in Java?

WBOY
WBOYforward
2023-08-20 18:13:06681browse

How can we avoid deadlock in Java?

In Java, Deadlock is a programming situation in which two or more threads are permanently blocked. A deadlock condition will occur between at least two threads and two or more resources .

How to avoid deadlock

  • Avoid nested locks: Deadlock mainly occurs when locks are provided to multiple threads. If we have already provided a lock to one thread, avoid providing locks to other threads.
  • Avoid unnecessary locks: We only need to provide locks to those necessary members. Providing locks unnecessarily can lead to deadlocks.
  • Use Thread.join(): A deadlock condition occurs when one thread is waiting for another thread to complete. If this happens, we can use Thread.join() and set the maximum execution time.

Example

public class DeadlockTest {
   public static void main(String[] args) throws InterruptedException {
      Object obj1 = new Object();
      Object obj2 = new Object();
      Object obj3 = new Object();
      Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1");
      Thread t2 = new Thread(new SyncThread(obj2, obj3), "t2");
      t1.start();
      Thread.sleep(2000);
      t2.start();
      Thread.sleep(2000);
   }
}
class SyncThread implements Runnable {
   private Object obj1;
   private Object obj2;
   public SyncThread(Object o1, Object o2){
      this.obj1=o1;
      this.obj2=o2;
   }
   @Override
   public void run() {
      String name = Thread.currentThread().getName();
      System.out.println(name + " acquiring lock on " + obj1);
      <strong>synchronized (obj1)</strong> {
         System.out.println(name + " acquired lock on " + obj1);
         work();
      }
      System.out.println(name + " released lock on " + obj1);
      System.out.println(name + " acquiring lock on " + obj2);
      <strong>synchronized (obj2)</strong> {
         System.out.println(name + " acquired lock on " + obj2);
         work();
      }
      System.out.println(name + " released lock on " + obj2);
      System.out.println(name + " finished execution.");
   }
   private void work() {
      try {
         Thread.sleep(5000);
      } catch (InterruptedException ie) {
         ie.printStackTrace();
      }
   }
}

Output

t1 acquiring lock on java.lang.Object@917d8d4
t1 acquired lock on java.lang.Object@917d8d4
t2 acquiring lock on java.lang.Object@5c4b42fb
t2 acquired lock on java.lang.Object@5c4b42fb
t1 released lock on java.lang.Object@917d8d4
t1 acquiring lock on java.lang.Object@5c4b42fb
t1 acquired lock on java.lang.Object@5c4b42fb
t2 released lock on java.lang.Object@5c4b42fb
t2 acquiring lock on java.lang.Object@528cb702
t2 acquired lock on java.lang.Object@528cb702
t1 released lock on java.lang.Object@5c4b42fb
t2 released lock on java.lang.Object@528cb702
t1 finished execution.
t2 finished execution.

The above is the detailed content of How can we avoid deadlock in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete