package step1; public class CreateThreadPractice { public static void main(String[] args) { MyThread myThread = new MyThread(); // ---------------------Begin------------------------ //开启线程 // ---------------------End------------------------ } } // ---------------------Begin------------------------ //继承Thread编写名为MyThead的类,代码内容为循环输出10遍: 线程在运行...... // ---------------------End------------------------
时间: 2023-08-15 14:41:08 浏览: 70
请看下面的代码:
```java
package step1;
public class CreateThreadPractice {
public static void main(String[] args) {
MyThread myThread = new MyThread();
// ---------------------Begin------------------------
//开启线程
myThread.start();
// ---------------------End------------------------
}
}
// ---------------------Begin------------------------
//继承Thread编写名为MyThead的类,代码内容为循环输出10遍: 线程在运行......
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("线程在运行......");
}
}
}
// ---------------------End------------------------
```
这段代码创建了一个名为 MyThread 的类,它继承自 Thread 类,实现了线程的功能。在主方法中,我们创建了 MyThread 的实例,并通过调用 start() 方法来启动线程。在 MyThread 类中,我们重写了 run() 方法,该方法定义了线程的行为。在本例中,我们只是循环输出了 10 次 "线程在运行......"。