I had just rubbed off the project code on my head, looked at the blog, and saw the thread thing. There was a little bit of writing about threads and doing bits and pieces. JAVA Basics – Process, thread safety

I look at it and think how can I write a deadlock? I open Eclipse and suddenly I don’t know where to start. It feels strange to write a deadlock instead of blocking and deadlocks…

Ok, to write a deadlock, you need to have a scenario and meet the deadlock conditions.

  • Mutually exclusive condition: a resource can only be used by one process at a time.
  • Request and hold conditions: when a process is blocked by requesting resources, it holds on to acquired resources
  • Non-dispossession condition: a process cannot forcibly take away a resource it has acquired until it is used up.
  • Circular waiting condition: a circular waiting resource relationship is formed between several processes.

First, there are competing resources, and both threads are waiting for each other to release resources at the same time. Let’s get two resources first:

Object lock=new Object();
Object lock2=new Object();
Copy the code

Then there are two threads:

Tr1 tr1=new Tr1(lock, lock2);
Tr2 tr2=new Tr2(lock, lock2);
		
Thread t1=new Thread(tr1);
Thread t2=new Thread(tr2);
Copy the code

Activation:

t1.start();
t2.start();
Copy the code

So for lock, how does lock2 create contention within threads? Look at the code:

package com.glmapper.base.synchronize;

public class Tr1 implements Runnable {
	
	Object lock;
	Object lock2;

	public Tr1(Object lock,Object lock2){
		this.lock= lock;
		this.lock2= lock2;
	}
	@Override
	public void runPrintln (thread.currentThread ().getName()+) {system.out.println (thread.currentThread ()"Obtain lock lock"); try { Thread.sleep(3000); } catch (Exception e) {} synchronized (lock2) {system.out.println (thread.currentThread ().getName()+"Lock2 lock obtained");
			}
		}
		
	}
}


public class Tr2 implements Runnable {
	
	Object lock;
	Object lock2;

	public Tr2(Object lock,Object lock2){
		this.lock= lock;
		this.lock2= lock2;
	}
	@Override
	public void runSystem.out.println(thread.currentThread ().getName()+"Lock2 lock obtained"); try { Thread.sleep(3000); } catch (Exception e) {} lock synchronized (lock) {system.out.println (thread.currentThread ().getName()+"Obtain lock lock"); }}}}Copy the code

When thread 1 acquires the lock, thread 2 acquires the lock2 lock. And then thread 1 continues, and I get here,

synchronized (lock2) {
	System.out.println(Thread.currentThread().getName()+"Lock2 lock obtained");
}
Copy the code

Lock2 is now held by thread 2; At the same time, thread 2 also starts executing to:

synchronized (lock) {
	System.out.println(Thread.currentThread().getName()+"Obtain lock lock");
}
Copy the code

Thread 2 is also trying to acquire the lock, but the lock is held by thread 1. Both threads are waiting for each other to release resources, causing a deadlock. OK, done… When I am ready to shut down, found still waiting?

  • Take a look at our process through JPS
  • Use jstack -l [pid] to view the information