你可以写一个java线程的创建嘛
时间: 2023-02-20 18:03:27 浏览: 51
当然可以!创建Java线程有两种方法:一种是通过实现Runnable接口,另一种是继承Thread类。
以下是通过实现Runnable接口创建线程的代码示例:
```
class MyRunnable implements Runnable {
public void run() {
System.out.println("Running in a thread");
}
}
public class Main {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
}
```
以下是通过继承Thread类创建线程的代码示例:
```
class MyThread extends Thread {
public void run() {
System.out.println("Running in a thread");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
```
您可以根据需要选择适合您的方法。希望以上代码示例对您有所帮助。