“Java中实现多线程”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第13行: | 第13行: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
=== 实现 Runnable 接口 === | === 实现 Runnable 接口 === |
2022年10月27日 (四) 05:55的版本
继承 Thread 类
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("i am thread");
}
public static void main(String[] args) {
new MyThread().start();
}
}
实现 Runnable 接口
public class MyThread implements Runnable {
@Override
public void run() {
System.out.println("i am thread");
}
public static void main(String[] args) {
Thread thread = new Thread(new MyThread());
thread.start();
}
}
其实 Thread 类就实现了 Runnable 接口
package java.lang;
public class Thread implements Runnable {
……
}