本帖最后由 不去会死 于 2014-6-25 22:36 编辑
- public class ThreadDieSock implements Runnable {
- private int flag = 1;
- private Object obj1 = new Object(), obj2 = new Object();
- public void run() {
- System.out.println("flag=" + flag);
- if (flag == 1) {
- synchronized (obj1) {
- System.out.println("我已经锁定obj1,休息0.5秒后锁定obj2去!");
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (obj2) {
- System.out.println("1");
- }
- }
- }
- if (flag == 0) {
- synchronized (obj2) {
- System.out.println("我已经锁定obj2,休息0.5秒后锁定obj1去!");
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (obj1) {
- System.out.println("0");
- }
- }
- }
- }
- public static void main(String[] args) {
- ThreadDieSock run01 = new ThreadDieSock();
- ThreadDieSock run02 = new ThreadDieSock();
- run01.flag = 1;
- run02.flag = 0;
- Thread thread01 = new Thread(run01);
- Thread thread02 = new Thread(run02);
- System.out.println("线程开始喽!");
- thread01.start();
- thread02.start();
- }
- }
复制代码 运行结果:
线程开始喽!
flag=0
flag=1
我已经锁定obj2,休息0.5秒后锁定obj1去!
我已经锁定obj1,休息0.5秒后锁定obj2去!
0
1
我的问题是,0 与 1 不是应该不会被打印的吗?有点懵了
|