hwb_java 2019-02-25 20:25 采纳率: 0%
浏览 687

为啥不是顺序执行的?

package test;

public class test {

public void f1() {
    new Thread() {
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("f1");
        }
    }.start();
}

public void f2() {
    new Thread() {
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("f2");
        }
    }.start();
}


public void f3() {
    new Thread() {
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("f3");
        }
    }.start();
}

synchronized void m1() {
    f1();
}
synchronized void m2() {
    f2();
}
synchronized void m3() {
    f3();
}
public static void main(String[] args) {
    test test = new test();
    test.m1();
    test.m2();
    test.m3();
}

}


  • 写回答

4条回答 默认 最新

  • 不了痕 2019-02-25 23:19
    关注

    多线程的执行和线程写的前后顺序无关,和CPU中的线程调度有关

    评论

报告相同问题?