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();
class MyCallable implements Callable<Integer> {
public Integer call() throws Exception {
// 线程执行的代码
return 1;
}
}
MyCallable callable = new MyCallable();
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(callable);
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.execute(new Runnable() {
public void run() {
// 线程执行的代码
}
});
这些方法都可以用来创建并启动一个新的线程,根据具体情况选择使用。