java编写一个程序,创建两个线程,要求分别输出26个字母。在输出结果时,要显示是哪个线程输出的字母。
时间: 2024-03-03 20:49:52 浏览: 67
好的,以下是符合您要求的Java程序:
```java
public class AlphabetPrinter {
public static void main(String[] args) {
Thread t1 = new Thread(new Printer('A', 'Z', "Thread 1"));
Thread t2 = new Thread(new Printer('a', 'z', "Thread 2"));
t1.start();
t2.start();
}
}
class Printer implements Runnable {
private char start;
private char end;
private String threadName;
public Printer(char start, char end, String threadName) {
this.start = start;
this.end = end;
this.threadName = threadName;
}
public void run() {
for (char c = start; c <= end; c++) {
System.out.println(threadName + " prints " + c);
}
}
}
```
这个程序会创建两个线程,每个线程分别输出小写或大写字母表。在输出每个字母时,程序会显示是哪个线程输出的。
阅读全文