Java中实现多线程的方法有以下几种:
示例代码:
public class MyThread extends Thread {
@Override
public void run() {
// 线程的任务
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
示例代码:
public class MyRunnable implements Runnable {
@Override
public void run() {
// 线程的任务
}
}
public class Main {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
}
示例代码:
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// 线程的任务
}
});
thread.start();
}
}
以上是Java中实现多线程的几种常见方法,每种方法都有其适用的场景,开发者可以根据具体需求选择合适的方法。