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