- 1.package namespace;
- 2.class Dead implements Runnable
- 3.{
- 4. public boolean flag;
- 5. public Object locka=new Object();
- 6. public Object lockb=new Object();
- 7. public Dead(boolean f) {this.flag=f;}
- 8. public void run()
- 9.
- 10. {
- 11. while(true) {
- 12. if(flag)
- 13. {
- 14. synchronized(Lock.locka)
- 15. {
- 16. System.out.println("if loca");
- 17. synchronized(Lock.lockb)
- 18. {
- 19. System.out.println("if lockb");
- 20. }
- 21. }
- 22. }
- 23.
- 24. else {
- 25. synchronized(Lock.lockb)
- 26. {
- 27. System.out.println("else lockb");
- 28. synchronized (Lock.locka) {
- 29. System.out.println("else locka");
- 30. }
- 31. }
- 32. }
- 33. }
- 34. }
- 35.}
- 36.class Lock
- 37.{
- 38. static Object locka=new Object();
- 39. static Object lockb=new Object();
- 40.}
- 41.public class DeadLock {
- 42.
- 43. public static void main(String args[])
- 44. {
- 45. Thread t1=new Thread(new Dead(true));
- 46. Thread t2=new Thread(new Dead(false));
- 47. t1.start();
- 48. t2.start();
- 49. }
- 50.}
复制代码 |
|