创建两个线程,要求如下: (1)一个线程输出100个1~26,另一个线程输出100个A~Z。 (2)一个线程使用集成Thread 类的写法,另一个线程使用实现Runnable接口的写法
时间: 2024-06-14 22:03:54 浏览: 15
以下是创建两个线程的示例代码:
1. 一个线程输出100个1~26,另一个线程输出100个A~Z
```java
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.print((char) (i % 26 + 64) + " ");
}
}
}
class LetterRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.print(i % 26 + 1 + " ");
}
}
}
public class Main {
public static void main(String[] args) {
NumberThread numberThread = new NumberThread();
Thread letterThread = new Thread(new LetterRunnable());
numberThread.start();
letterThread.start();
}
}
```
相关问题
创建两个线程,要求如下: (1)一个线程输出100个1~26,另一个线程输出100个A~Z。 (2)一个线程使用集成Thread 类的写法,另一个线程使用实现Runnable接口的写法。
以下是创建两个线程的示例代码:
1. 一个线程输出100个1~26,另一个线程输出100个A~Z。
```java
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.print(i " ");
}
}
}
class LetterThread implements Runnable {
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + " ");
}
}
}
public class Main {
public static void main(String[] args) {
NumberThread numberThread = new NumberThread();
Thread letterThread = new Thread(new LetterThread());
numberThread.start();
letterThread.start();
}
}
```
2. 一个线程使用集成Thread类的写法,另一个线程使用实现Runnable接口的写法。
```java
class NumberThread extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.print(i + " ");
}
}
}
class LetterThread implements Runnable {
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + " ");
}
}
}
public class Main {
public static void main(String[] args) {
NumberThread numberThread = new NumberThread();
Thread letterThread = new Thread(new LetterThread());
numberThread.start();
letterThread.start();
}
}
```
创建两个线程,要求如下: (1)一个线程输出100个1~26,另一个线程输出100个A~Z。 (2)一个线程使用集成Thread 类的写法,另一个线程使用实现Runnable接口的写法。
以下是创建两个线程的示例代码:
1. 使用继承Thread类的写法:
```java
class FirstThread extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.print(i + " ");
}
}
}
class SecondThread extends Thread {
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + " ");
}
}
}
public static void main(String[] args) {
FirstThread firstThread = new FirstThread();
SecondThread secondThread = new SecondThread();
firstThread.start();
secondThread.start();
}
```
2. 使用实现Runnable接口的写法:
```java
class FirstThread implements Runnable {
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.print(i + " ");
}
}
}
class SecondThread implements Runnable {
public void run() {
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c + " ");
}
}
}
public static void main(String[] args) {
FirstThread firstThread = new FirstThread();
SecondThread secondThread = new SecondThread();
Thread thread1 = new Thread(firstThread);
Thread thread2 = new Thread(secondThread);
thread1.start();
thread2.start();
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)