- class Lock implements Runnable {
- private static int x = 1000;
- boolean flag ;
- public Lock(boolean flag) {
- this.flag = flag;
- }
-
- static Object o1 = new Object();
- static Object o2 = new Object();
-
-
- public void run() {
- while(true) {
-
- if(flag) {
- synchronized(o1) {
- synchronized(o2){
- System.out.println(flag + "***************" + x--);
- }
- }
- } else{
- synchronized(o2) {
- synchronized(o1) {
- System.out.println(flag + "&&&&&&&&&&&&&&&" + x--);
- }
- }
- }
- }
- }
- }
- public class DeathLock {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
-
- Thread t1 = new Thread(new Lock(true));
- Thread t2 = new Thread(new Lock(false));
- t1.start();
- t2.start();
- }
- }
复制代码 |
|