Java創建多線程的方式有以下幾種:
class MyThread extends Thread {
public void run() {
// 線程執行的代碼
}
}
MyThread thread = new MyThread();
thread.start();
class MyRunnable implements Runnable {
public void run() {
// 線程執行的代碼
}
}
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
Thread thread = new Thread(new Runnable() {
public void run() {
// 線程執行的代碼
}
});
thread.start();
ExecutorService executor = Executors.newFixedThreadPool(10);
executor.execute(new Runnable() {
public void run() {
// 線程執行的代碼
}
});
executor.shutdown();