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();
}
}