牛客网java

梦想的初衷 提交于 2020-01-10 11:45:52

1:下列代码运行的结果是什么?

public class P {
    public static int abc = 123;
    static{
        System.out.println("P is init");
    }
}
public class S extends P {
    static{
        System.out.println("S is init");
    }
}
public class Test {
    public static void main(String[] args) {
        System.out.println(S.abc);
    }
}
View Code

 

 原因:

 2:下面程序的运行结果是:(    )

public static void main(String args[]) {

        Thread t = new Thread() {
            public void run() {
                pong();
            }
        };

        t.run();
        System.out.print("ping");

    }

    static void pong() {
        System.out.print("pong");
    }
View Code

 

 解释:并不是多线程竞争问题,就是正常执行,t.run是调用的Thead类中的run()方法,t.start才是执行线程,所以这题就是执行普通run()方法,先输出pong,在输出ping。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!