- public class DeadLock implements Runnable {
- boolean flag;
- public DeadLock(boolean flag) {
- this.flag = flag;
- }
- //相互持锁,互不释放,产生死锁
- public void run() {
- while(true){
- if(flag){
- synchronized(MyLock.lock1){
- System.out.println("true lock1");
- synchronized (MyLock.lock2) {
- System.out.println("false lock2");
- }
- }
- }else{
- synchronized(MyLock.lock2){
- System.out.println("false lock2");
- synchronized(MyLock.lock1){
- System.out.println("false lock1");
- }
- }
- }
- }
-
- }
- }
复制代码
|