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();
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java创建线程的方法是什么