用java写一段程序,实现两个线程循环打印
时间: 2023-09-05 16:04:15 浏览: 89
### 回答1:
```
class PrinterThread extends Thread {
private String name;
public PrinterThread(String name) {
this.name = name;
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(name + ": " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
PrinterThread t1 = new PrinterThread("Thread 1");
PrinterThread t2 = new PrinterThread("Thread 2");
t1.start();
t2.start();
}
}
```
该代码创建了两个线程`PrinterThread t1`和`PrinterThread t2`,并在`run`方法中实现了循环打印。每个线程在每次循环打印后休眠100毫秒,以让另一个线程有机会执行。
### 回答2:
可以使用Java的多线程机制来实现两个线程循环打印的功能。以下是示例代码:
```java
public class LoopPrinting {
private static final Object lock = new Object();
private static int num = 1;
private static int count = 1;
public static void main(String[] args) {
Thread threadA = new Thread(new PrintThread("A", 1));
Thread threadB = new Thread(new PrintThread("B", 2));
threadA.start();
threadB.start();
}
static class PrintThread implements Runnable {
private String threadName;
private int threadNumber;
public PrintThread(String threadName, int threadNumber) {
this.threadName = threadName;
this.threadNumber = threadNumber;
}
@Override
public void run() {
while (num <= 100) {
synchronized (lock) {
if (count % 3 == threadNumber - 1) {
System.out.println(threadName + ":" + num);
num++;
count++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
```
在上述代码中,定义了一个共享的锁对象`lock`,一个共享的计数变量`num`和一个判断打印顺序的变量`count`。两个线程`ThreadA`和`ThreadB`通过`PrintThread`类实例化,并传入不同的线程名和线程编号。
在`PrintThread`的`run`方法中,使用`synchronized`关键字锁定`lock`对象,保证在每个线程执行时只有一个线程能够访问共享资源。通过判断`count`变量的值,实现两个线程循环打印。其中`count % 3`的结果等于`threadNumber`减一,表示该线程可以进行打印操作。
当线程打印完一个数字后,更新`num`和`count`的值,并使用`lock.notifyAll()`唤醒其他正在等待的线程。然后当前线程释放锁并进入等待状态,直到其他线程唤醒它。
### 回答3:
可以使用Java中的多线程编程来实现两个线程的循环打印。以下是一个简单的示例代码:
```java
public class TwoThreadPrinting {
private static final Object lock = new Object();
private static volatile boolean isThread1Turn = true; // 标识线程1是否应该打印
public static void main(String[] args) {
Thread thread1 = new Thread(new PrintThread(1));
Thread thread2 = new Thread(new PrintThread(2));
thread1.start();
thread2.start();
}
private static class PrintThread implements Runnable {
private final int threadNumber;
public PrintThread(int threadNumber) {
this.threadNumber = threadNumber;
}
@Override
public void run() {
try {
for (int i = 1; i <= 10; i++) {
synchronized (lock) {
// 如果当前线程不是轮到自己打印,则当前线程等待
while ((threadNumber == 1 && !isThread1Turn) || (threadNumber == 2 && isThread1Turn)) {
lock.wait();
}
System.out.println("Thread " + threadNumber + ": " + i);
isThread1Turn = !isThread1Turn; // 切换下一个线程的打印权限
lock.notifyAll(); // 唤醒所有等待的线程
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在上述代码中,我们定义了一个`TwoThreadPrinting`类,并在该类中创建了两个线程对象`thread1`和`thread2`。每个线程都被分配了一个`PrintThread`实例,其中`PrintThread`实现了`Runnable`接口,并且重写了`run`方法。在`run`方法中,通过`synchronized`关键字和`wait`、`notifyAll`方法来实现线程的同步与等待。
在程序运行过程中,每个线程都会进行循环打印10次,打印的内容格式为`Thread x: y`,其中`x`表示线程编号,`y`表示打印的数字。线程1在打印完当前数字后,会将`isThread1Turn`设置为`false`,然后唤醒线程2;而线程2在打印完当前数字后,会将`isThread1Turn`设置为`true`,然后唤醒线程1。这样两个线程就能够循环打印了。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)