Java编程,创建两个线程,一个线程通过创建Thread类的子类来构造线程,打印英文字母“A”-“z”,一个通过实现一个Runnable接口的类来构造线程,打印0-100。两种方式都要实现。
时间: 2024-02-23 12:02:53 浏览: 131
好的,我来为您提供代码示例:
通过创建Thread类的子类来构造线程,打印英文字母“A”-“z”:
```java
class PrintLettersThread extends Thread {
@Override
public void run() {
for (char c = 'A'; c <= 'z'; c++) {
System.out.print(c + " ");
}
}
}
public class Main {
public static void main(String[] args) {
PrintLettersThread thread1 = new PrintLettersThread();
thread1.start();
}
}
```
通过实现一个Runnable接口的类来构造线程,打印0-100:
```java
class PrintNumbersRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
System.out.print(i + " ");
}
}
}
public class Main {
public static void main(String[] args) {
PrintNumbersRunnable printNumbersRunnable = new PrintNumbersRunnable();
Thread thread2 = new Thread(printNumbersRunnable);
thread2.start();
}
}
```
两种方式都实现了,您可以参考以上代码。
阅读全文