1. class MyThread extends Thread { public void run() { // 线程执行的代码 }}// 在主函数中创建并启动线程MyThread thread = new MyThread();thread.start(); 2. class MyRunnable implements Runnable { public void run() { // 线程执行的代码 }}// 在主函数中创建并启动线程MyRunnable runnable = new MyRunnable();Thread thread = new Thread(runnable);thread.start(); 3. class PrintThread extends Thread { private String message; private int count; private int sleepTime; public PrintThread(String message, int count, int sleepTime) { this.message = message; this.count = count; this.sleepTime = sleepTime; } public void run() { for (int i = 0; i < count; i++) { System.out.print(message); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } }}// 在主函数中创建并启动线程PrintThread threadA = new PrintThread("a", 10, 100);PrintThread threadB = new PrintThread("b", 10, 200);threadA.start();threadB.start();分析这三个代码,并解释它们是如何实现的
时间: 2024-01-01 09:05:53 浏览: 27
这三段代码都是用来创建和启动线程的。其中,第一段代码是继承了Thread类,重写了run()方法;第二段代码是实现了Runnable口,重写了run()方法;第三段代码也是继承了Thread类,但是构造函数中传入了一些参数,使得线程在运行时可以打印指定的字符串、次数和间隔时间。
在第一段代码中,创建一个MyThread类的实例对象thread,然后调用start()方法启动线程。线程执行的代码在重写的run()方法中实现。
在第二段代码中,创建了一个MyRunnable类的实例对象runnable,然后将其作为参数传递给Thread类的构造函数,创建一个Thread类的实例对象thread,并调用start()方法启动线程。线程执行的代码在重写的run()方法中实现。
在第三段代码中,创建了两个PrintThread类的实例对象threadA和threadB,构造函数中传入了"a"和"b"两个字符串、10次打印和不同的间隔时间。然后分别调用start()方法启动线程。线程执行的代码在重写的run()方法中实现,在循环中打印指定的字符串、休眠指定的时间。
相关问题
1.将下图代码保存到该ThreadDemo.java中。查阅API文档等,为代码添加注释。 功能:分别以两种方式创建线程对象 class MyThread extends Thread { public void run() { // System.out.println("线程1运行了。"); } } class MyRunnable implements Runnable {// public void run() { System.out.println("线程2运行了。"); } } public class ThreadDemo { // public static void main(String[] args) { Thread t1 = new MyThread(); // Runnable target = new MyRunnable(); // Thread t2 = new Thread(target); // // 创建线程对象t3(本质上仍是方式2,但使用了Lambda表达式以简化代码) Thread t3 = new Thread(() -> System.out.println("线程3运行了。")); t1.start(); // t2.start(); // t3.start(); // } }
```
// 定义一个继承自Thread类的线程
class MyThread extends Thread {
// 重写Thread类的run方法
public void run() {
// 打印信息
// System.out.println("线程1运行了。");
}
}
// 定义一个实现了Runnable接口的线程
class MyRunnable implements Runnable {
// 实现Runnable接口的run方法
public void run() {
// 打印信息
System.out.println("线程2运行了。");
}
}
// 主类
public class ThreadDemo {
public static void main(String[] args) {
// 创建线程对象t1,该线程是继承自Thread类的线程
Thread t1 = new MyThread();
// 创建线程对象t2,该线程是实现了Runnable接口的线程
Runnable target = new MyRunnable();
Thread t2 = new Thread(target);
// 创建线程对象t3,使用Lambda表达式简化代码
Thread t3 = new Thread(() -> System.out.println("线程3运行了。"));
// 开始运行线程
t1.start();
t2.start();
t3.start();
}
}
```
注释已经添加在代码中了。
package step1; import java.lang.Thread; public class MyThread extends Thread { private int num;//任务就是在子线程中计算num的阶乘 public MyThread() { this(0); } //constructor,创建实例的时候初始化参数 public MyThread(int num) { /begin your code here/ num=0; /end your code/ } @Override public void run() { //重写run方法,在子线程中想要执行的代码写在run方法中 int result = 1;//result保存计算出的结果 /begin your code here/ for(int i=1;i<=num;i++) { result*=i; } /end your code/ //直接输出结果 System.out.println(result); } }
好的,我来帮您补全代码。在构造函数中,应该将传入的参数赋值给成员变量num,而不是将成员变量num赋值为0。因此,需要修改如下:
```
public MyThread(int num) {
/begin your code here/
this.num=num;
/end your code/
}
```
在for循环中,需要将i从1循环到num,而不是从0循环到num-1,因此需要修改如下:
```
/begin your code here/
for(int i=1;i<=num;i++)
{
result*=i;
}
/end your code/
```
这样就完成了代码的补全。完整代码如下所示:
```
package step1;
import java.lang.Thread;
public class MyThread extends Thread {
private int num;//任务就是在子线程中计算num的阶乘
public MyThread() {
this(0);
}
//constructor,创建实例的时候初始化参数
public MyThread(int num) {
/begin your code here/
this.num=num;
/end your code/
}
@Override
public void run() {
//重写run方法,在子线程中想要执行的代码写在run方法中
int result = 1;//result保存计算出的结果
/begin your code here/
for(int i=1;i<=num;i++)
{
result*=i;
}
/end your code/
//直接输出结果
System.out.println(result);
}
}
```