Java实现多线程有两种途径:继承Thread类和实现Runnable接口。
示例代码如下:
public class MyThread extends Thread {
@Override
public void run() {
// 线程的任务
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
示例代码如下:
public class MyRunnable implements Runnable {
@Override
public void run() {
// 线程的任务
}
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
区别:
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java可以创建哪两种线程