- package practice;
- /**
- * 死锁程序
- * @author Qihuan
- *
- */
- class DeadLock implements Runnable{
- private boolean flag;
- public DeadLock(boolean flag) {
- // TODO Auto-generated constructor stub
- this.flag = flag;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- if (flag) {
- synchronized (MyLock.lockA) {
- System.out.println("if lockA");
- synchronized (MyLock.lockB) {
- System.out.println("if lockB");
- }
- }
- }else{
- synchronized (MyLock.lockB) {
- System.out.println("else lockB");
- synchronized (MyLock.lockA) {
- System.out.println("else lockA");
- }
- }
- }
- }
- }
- class MyLock {
- static Object lockA = new Object();
- static Object lockB = new Object();
- }
- public class DeadLockTest {
- public static void main(String[] args) {
- Thread t = new Thread(new DeadLock(true));
- Thread t2 = new Thread(new DeadLock(false));
- t.start();
- t2.start();
- }
- }
复制代码
|
|