从我在Java中使用线程的时间开始,我发现了这两种编写线程的方法:
用implements Runnable:
public class MyRunnable implements Runnable {
public void run() {
//Code
}
}
//Started with a "new Thread(new MyRunnable()).start()" call
Run Code Online (Sandbox Code Playgroud)
或者,用extends Thread:
public class MyThread extends Thread {
public MyThread() {
super("MyThread");
}
public void run() {
//Code
}
}
//Started with a "new MyThread().start()" call
Run Code Online (Sandbox Code Playgroud)
这两个代码块有什么显着差异吗?
我正在调试一些代码.我的调试器显示代码的来源来自Thread.run().我需要知道调用Thread.start()代码的哪一部分!有没有办法找到这个?
我知道具体的 Thread 类和 Java 中的 Runnable Interface 之间的区别。需要什么使 Thread 类可重写以便可供使用?所有线程都可以通过实现Runnable接口来创建吗?需要 Thread 类的用例是什么?为什么我们有两种方法来解决同一个问题?
编辑:我知道 Thread 类是 Runnable 实现的容器,我想知道是否有任何用例是 Runnable 实现无法解决的