第一种:synchronized
package gstation.zerotech.javademo;
import sun.rmi.runtime.Log;
/**
* 线程的状态 new 就绪 running wait(阻塞) terminal(结束)
* Created by admin on 2018/5/18.
*/
public class SyncDeadLock {
private static Object locka = new Object();
private static Object lockb = new Object();
public void deadLock(){
final Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (locka){
try {
System.out.println(Thread.currentThread().getName() + " get locka ing!");
//进入休眠状态,但是不释放锁
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + " after sleep 500ms!");
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
//进入等待,并且释放当前锁
locka.wait();//释放锁,进入无线等待,防止死锁
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " need lockb! Just waiting!");
synchronized (lockb){
System.out.println(Thread.currentThread().getName() + "get lockb ing!");
}
}
}
},"thread1");
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lockb){
try {
System.out.println(Thread.currentThread().getName() + " get lockb ing!");
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + " after sleep 500s!");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " need locka! Just waiting!");
synchronized (locka){
System.out.println(Thread.currentThread().getName() + " get locka ing!");
locka.notify();//执行完唤醒locka的等待者
}
}
}
},"thread2");
thread1.start();
thread2.start();
}
}
第二种: Lock
package gstation.zerotech.javademo;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by admin on 2018/5/18.
*/
public class LockDeadDemo {
public void test(){
final DeadLockBean deadLockBean = new DeadLockBean();
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
deadLockBean.productDeadLock();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
},"threadA");
Thread threadB = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(310);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
deadLockBean.productDeadLock();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
},"threadB");
threadA.start();
threadB.start();
try {
System.out.println("main will join");
threadA.join();
threadB.join();
System.out.println("main recover from join");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class DeadLockBean{
private Lock lock = new ReentrantLock();
public void productDeadLock() throws Throwable {
System.out.println(Thread.currentThread().getName() + " in method");
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + " excute method");
// throw new Throwable("paochu Throwable!");
throw new Exception("paochu yichang");
} catch (Exception e) {
System.out.println(Thread.currentThread().getName() + " shasheng catch!");
// lock.unlock();
}finally {
System.out.println(Thread.currentThread().getName() + " fasheng finally!");
lock.unlock();//无论发生任何异常都会释放锁
}
System.out.println(Thread.currentThread().getName() + " trycatch shifang lock");
}
}
}