Java创建多个子线程的方法可以通过以下两种方式实现:
public class MyThread extends Thread {
public void run() {
// 线程要执行的逻辑
}
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
}
}
public class MyRunnable implements Runnable {
public void run() {
// 线程要执行的逻辑
}
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread1 = new Thread(myRunnable);
Thread thread2 = new Thread(myRunnable);
thread1.start();
thread2.start();
}
}