Java死锁
产生死锁原因:
1.系统资源不足.
2.进程运行推进的顺序不合适
3.资源分配不当
import java.util.concurrent.TimeUnit; class HoldLockThread implements Runnable { private String lockA; private String lockB; public HoldLockThread(String lockA, String lockB) { this.lockA = lockA; this.lockB = lockB; } @Override public void run() { synchronized (lockA) { System.out.println(Thread.currentThread().getName() + "\t 自己持有:" + lockA + "\t 尝试获得:" + lockB); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println(Thread.currentThread().getName() + "\t 自己持有:" + lockB + "\t 尝试获得:" + lockA); } } } } public class DeadLockDemo { public static void main(String[] args) { String lockA = "lockA"; String lockB="lockB"; new Thread(new HoldLockThread(lockA,lockB),"ThreadAA").start(); new Thread(new HoldLockThread(lockB,lockA),"ThreadBB").start(); } }
解决:
JVM体系:
常见的垃圾回收算法:
引用计数:
复制:
标记清除:
标记整理: