Demonstrate deadlocks and how to resolve them

Demonstrate deadlock using lock

use of package lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class deadlock {
	public static void main(String[] args) {
		
		Lock lockA = new ReentrantLock();
		Lock lockB = new ReentrantLock();
		
		Thread t1 = new Thread() {
			@Override
			public void run() {
				System.out.println(this.getName()+"thread start");
				System.out.println(this.getName()+"The view occupies lockA");
				lockA.lock ();
				System.out.println(this.getName()+"Occupy lockA successfully");
				try {
					//Execute business logic for 5 seconds and wait for other threads to occupy lockB
					Thread.sleep(5000);
					
					System.out.println(this.getName()+"The view occupies lockB");
					lockB.lock();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace ();
				}finally {
					System.out.println(this.getName()+"thread end");
					lockA.unlock ();
				}
			}
		};
		t1.setName("T1");
		t1.start();
		
		Thread t2 = new Thread() {
			@Override
			public void run() {
				System.out.println(this.getName()+"thread start");
				System.out.println(this.getName()+"The view occupies lockB");
				lockB.lock();
				System.out.println(this.getName()+"Occupy lockB successfully");
				try {
					//Execute business logic for 5 seconds and wait for other threads to occupy lockA
					Thread.sleep(5000);
					
					System.out.println(this.getName()+"The view occupies lockA");
					lockA.lock ();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace ();
				}finally {
					System.out.println(this.getName()+"thread end");
					lockB.unlock();
				}
			}
		};
		t2.setName("T2");
		t2.start();
	}
}

The following code uses trylock() to solve the deadlock problem

use of package lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class deadlock {
	public static void main(String[] args) {
		
		Lock lockA = new ReentrantLock();
		Lock lockB = new ReentrantLock();
		
		Thread t1 = new Thread() {
			@Override
			public void run() {
				/ / Determine whether to occupy the successful boolean value
				boolean locked = false;
				System.out.println(this.getName()+"thread start");
				System.out.println(this.getName()+"The view occupies lockA");
				lockA.lock ();
				System.out.println(this.getName()+"Occupy lockA successfully");
				try {
					//Execute business logic for 5 seconds and wait for other threads to occupy lockB
					Thread.sleep(5000);
					
					System.out.println(this.getName()+"The view occupies lockB");
					//Use trylock() below to occupy lockB when the T1 thread has already occupied lockA, and trylock() will let go when the specified time is not occupied to avoid deadlock (line 29, line 38)
					//Use trylock() to occupy, give two seconds to grab resources
					locked = lockB.tryLock(2,TimeUnit.SECONDS);
					if(locked) {
						//If the occupation is successful, execute the logic for five seconds
						System.out.println(this.getName()+"Thread occupied lockB successfully");
						Thread.sleep(5000);
						System.out.println(this.getName()+"The thread occupies lockB and ends");
						lockB.unlock();
					}else {
						System.out.println(this.getName()+"The thread spent two seconds occupying lockB unsuccessfully");
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace ();
				}finally {
					System.out.println(this.getName()+"thread end");
					lockA.unlock ();
				}
			}
		};
		t1.setName("T1");
		t1.start();
		
		Thread t2 = new Thread() {
			@Override
			public void run() {
				/ / Determine whether to occupy the successful boolean value
				boolean locked = false;
				
				System.out.println(this.getName()+"thread start");
				System.out.println(this.getName()+"The view occupies lockB");
				lockB.lock();
				System.out.println(this.getName()+"Occupy lockB successfully");
				try {
					//Execute business logic for 5 seconds and wait for other threads to occupy lockA
					Thread.sleep(5000);
					
					System.out.println(this.getName()+"The view occupies lockA");
					//Use trylock() below to occupy lockA when the T2 thread has already occupied lockB, and trylock() will let go if it is not occupied by the specified time to avoid deadlock (line 68, line 78)
					//Use trylock() to occupy, give two seconds to grab resources
					locked = lockA.tryLock(2,TimeUnit.SECONDS);
					if(locked) {
						//If the occupation is successful, execute the logic for five seconds
						System.out.println(this.getName()+"The thread occupies lockA successfully");
						Thread.sleep(5000);
						System.out.println(this.getName()+"The thread occupies lockA and ends");
						// end of thread
						lockA.unlock ();
					}else {
						System.out.println(this.getName()+"The thread spent two seconds occupying lockA unsuccessfully");
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace ();
				}finally {
					System.out.println(this.getName()+"thread end");
					lockB.unlock();
				}
			}
		};
		t2.setName("T2");
		t2.start();
	}
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522417&siteId=291194637