- class DeadLockDemo {
-
- public static String s1 = "筷子";
- public static String s2 = "刀叉";
-
- public static void main(String[] args) {
- new Thread(){
- public void run() {
- synchronized(s1) {
- System.out.println(getName() + ": 拿到了筷子, 等待刀叉");
- synchronized(s2) {
- System.out.println(getName() + ": 拿到了刀叉");
- }
- }
- }
- }.start();
-
-
- new Thread(){
- public void run() {
- synchronized(s2) {
- System.out.println(getName() + ": 拿到了刀叉, 等待筷子");
- synchronized(s1) {
- System.out.println(getName() + ": 拿到了筷子");
- }
- }
- }
- }.start();
- }
-
- }
复制代码 这也是个死锁 |