日期:2014-05-20  浏览次数:20634 次

进程有不理解的地方.执行顺序的问题

public class Test{

public static void main(String args[])   {
LogOaRentThread2 lt = new LogOaRentThread2();
lt.start();
String ss = "auto mapping";
final String s = ss;
new Thread(){
public void run(){
System.out.println("start");
doMapping(s);
System.out.println("end");
}
}.start();

System.out.println(getBoolean());
}


public static  boolean getBoolean(){
return false;
}

public static synchronized void doMapping(String ss){
System.out.println(ss);
}


static class LogOaRentThread2 extends Thread{
public void run (){
System.out.println("LogOaRentThread2 start");
System.out.println("do something");
System.out.println("LogOaRentThread2 end");
}
}

}


Console :

LogOaRentThread2 start
do something
LogOaRentThread2 end
false
start
auto mapping
end
为什么new出来的对象,不是异步的呢?
Java 线程

------解决方案--------------------
你把代码修改一下,你就明白了:
public class Test{
     
    public static void main(String args[])   {
        LogOaRentThread2 lt = new LogOaRentThread2();
        lt.start();
        String ss = "auto mapping";
        final String s = ss;
        new Thread(){
            public void run(){
                System.out.println("start");
                doMapping(s);
                System.out.println("end");
            }
        }.start();
        
        System.out.println(getBoolean());
    }
    public static  boolean getBoolean(){
        return false;
    }
     
    public static synchronized void doMapping(String ss){
        Thread.currentThread().sleep(5000); // 这块要加trycatch
        System.out.println(ss);
    }