- @Override
- public void run() {
- if (b) {
- synchronized (LockeInterface.objA) {
- System.out.println("true objA 进来了");
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (LockeInterface.objB) {
- System.out.println("true objB 进来了");
- }
- }
- } else {
- synchronized (LockeInterface.objB) {
- System.out.println("false objB 进来了");
- synchronized (LockeInterface.objA) {
- System.out.println("false objA 进来了");
- }
- }//
- }
- }
- }
- public class MyTest {
- public static void main(String[] args) {
- // 死锁:
- // 两个或者两个以上的线程, 在抢占 CPU 的执行权的时候, 都处于等待状态
- MyThread th1 = new MyThread(true);
- MyThread th2 = new MyThread(false);
- th1.start();
- th2.start();
- }
- }
来源: http://www.bubuko.com/infodetail-3068953.html