面试题:运行下列程序, 会产生什么结果( )public class X extends Thread implements Runnable{
public void run(){
System.out.println("this is run()");
}
public static void main(String[] args){
Thread t=new Thread(new X());
t.start();
}
}A.第一行会产生编译错误
B.第六行会产生编译错误
C.第六行会产生运行错误
D.程序会运行和启动创建线程不是有两种方法:
(1)实现Runnable接口
(2)继承Thread类这么这种方法也行?
public void run(){
System.out.println("this is run()");
}
public static void main(String[] args){
Thread t=new Thread(new X());
t.start();
}
}A.第一行会产生编译错误
B.第六行会产生编译错误
C.第六行会产生运行错误
D.程序会运行和启动创建线程不是有两种方法:
(1)实现Runnable接口
(2)继承Thread类这么这种方法也行?
解决方案 »
- 为什么我的降序没什么效果求大牛指点菜鸟在此谢了
- List<Integer> list ; 中的尖括号是什么意思?
- 大家有什么好的文档管理工具?比如变来变去的需求文档?
- 对一个有数字签名的JAR文件,如何解包呀.
- 求两个日期之间的天数时出了错
- 求java外观宣染包?? 急急~~~~
- OutputStream的方法void write(int b)为什么不设计成void write(byte b)呢?
- 个人兴趣模拟c#DataTable类实现的Java DataTable,供参考交流。
- ##### java 网络编程使用 socks 代理的问题 #####
- 如何在当前目录下创建一个目录?
- 求《Visual C++精彩实例详解》光盘
- java socket的半关闭
public void run(){
System.out.println( "this is run() ");
}
public static void main(String[] args){
Thread t=new Thread(new X()); //这种写法是把X当作Runnable类型来处理的 /**
* 也可以这样写
*/
Thread tt = new X() //这种写法是把X当作Thread类型来处理 t.start();
tt.start();
}
}
public void run() {
System.out.println("this is run() ");
} public static void main(String[] args) {
Thread t = new Thread(new X()); // 这句是核心, 构造一个线程, new X() 中的 X只要实现Runnable接口即可, 并将在t的调用X.run()方法.
t.start();
}
}
// 其中
Thread t = new Thread(new X());
这句是核心, 构造一个线程, new X() 中的 X只要实现Runnable接口即可, 并将在t的start方法中,直接调用X.run()方法.
实际上, 简单理解就是t的实际运行对象是new X(),而X只需实现Runnable接口.So. 上述代码可以正确运行, 结果就是
this is run()
Thread 是怎么搞的
1 搞一个类implements Runnable
2 new 出这个类
3 new 出thread(将这个类放入到这里)
4 new 出thread(将这个类放入到这里).start()方法
搞Thread 只要晓的这4个,什么都晓的了
public void run(){
System.out.println("this is run()");
}
public static void main(String[] args){
Thread t=new Thread(new X());
t.start();
}
} 在我这里编译运行都没有问题(是不是你的包没写对)创建线程不是有两种方法:
(1)实现Runnable接口
(2)继承Thread类 只要用其中一种就可以了,
public class X extends Thread implements Runnable{
像你这样Thread就被重载了.