java多线程创建有哪些方法
在Java中,有两种方法可以创建多线程:
public class MyThread extends Thread {
public void run() {
// 线程履行逻辑
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
public class MyRunnable implements Runnable {
public void run() {
// 线程履行逻辑
}
}
public class Main {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
这两种方法都可以用来创建多线程,但是推荐使用实现Runnable接口的方法,由于Java只支持单继承,如果继承了Thread类就没法继承其他类了,而实现Runnable接口可以免这个问题。
tiktok粉丝购买:https://www.smmfensi.com/
TOP