java - 使用 thread1 打印数字 1,2,3,使用 thread2 打印数字 4,5,6,使用 thread3 打印数字 7,8,9,再使用 thread1 打印 10,11,12
问题描述
我正在尝试编写一个带有等待和通知的简单程序,我将在其中创建 3 个线程。
第一个线程应该打印 1、2、3。
第二个线程应该打印 4、5、6。
第三个线程应该打印 7、8、9。
之后,第一个线程应该打印 10、11、12 等等。
下面是同一练习的示例代码,但我无法打印所需的输出。
public class MyThread2 extends Thread {
public final static Object obj = new Object();
int threadNo;
static volatile int threadNoToRun;
static volatile int counter = 1;
public MyThread2(int threadNo){
this.threadNo= threadNo;
}
@Override
public void run() {
synchronized (obj) {
try {
if(threadNoToRun != threadNo)
obj.wait();
else{
for(int i = 1 ; i < 4 ; i++){
if(threadNoToRun == threadNo){
System.out.println(threadNo + " counter value is "+counter);
counter++;
System.out.println(threadNo + " counter value is "+counter);
counter++;
System.out.println(threadNo + " counter value is "+counter);
counter++;
if(threadNoToRun == 1){
threadNoToRun = 2;
}
else if(threadNoToRun == 2){
threadNoToRun = 3;
}
else if(threadNoToRun == 3){
threadNoToRun = 1;
}
}
}
obj.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main (String args[]) {
/*
* Creating as many threads as needed.
*/
MyThread2 th1 = new MyThread2(1);
MyThread2 th2 = new MyThread2(2);
MyThread2 th3 = new MyThread2(3);
MyThread2.threadNoToRun = 1;
th1.start();
th2.start();
th3.start();
}
}
输出如下所示:
1 counter value is 1
1 counter value is 2
1 counter value is 3
2 counter value is 4
2 counter value is 5
2 counter value is 6
解决方案
在这里,只是一些变化。
尽管如此,我必须指出,这种并发不会提高计算速度。始终只有一个线程处于活动状态。
public class MyThread2 extends Thread {
public final static Object obj = new Object();
int threadNo;
static volatile int threadNoToRun;
static volatile int counter = 1;
public MyThread2(int threadNo) {
this.threadNo = threadNo;
}
@Override
public void run() {
synchronized (obj) {
try {
while (counter < 100) {
if (threadNoToRun != threadNo)
obj.wait();
else {
System.out.println(threadNo + " counter value is " + counter);
counter++;
System.out.println(threadNo + " counter value is " + counter);
counter++;
System.out.println(threadNo + " counter value is " + counter);
counter++;
if (threadNoToRun == 1) {
threadNoToRun = 2;
} else if (threadNoToRun == 2) {
threadNoToRun = 3;
} else if (threadNoToRun == 3) {
threadNoToRun = 1;
}
obj.notifyAll();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
/*
* Creating as many threads as needed.
*/
MyThread2 th1 = new MyThread2(1);
MyThread2 th2 = new MyThread2(2);
MyThread2 th3 = new MyThread2(3);
MyThread2.threadNoToRun = 1;
th1.start();
th2.start();
th3.start();
}
}
推荐阅读
- sql - Firebase BigQuery,在触发“app_remove”之前获取 event_name
- javascript - 如何使用简单的 html 在 textarea 中保留换行符?
- ios - 无法在 iOS 设备上发送 UDP 广播 - NWConnection
- google-cloud-platform - 使用 Google 自然问题 (NQ) 数据集
- redis - 有什么方法可以关闭空闲的 redis-client 连接?
- excel - 需要将文件保存在特定位置
- bash - 如何在bash中的多行字符串变量的每一行上运行命令?
- php - 如何在所有虚拟主机中设置 php?
- javascript - react.js 如何在触发某些操作时重新渲染 App.js
- python - 如何在 Python 中使用 C API 获取对象 ID?