- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个 java 程序,打印经过的秒数,每 5 秒打印一条消息。这是一个示例输出:
0 1 2 3 4 hello 5 6 7 8 9 hello 10 11 12 13 14 hello 15 16 17 18 19 hello
如何删除 boolean 变量 printMsg?有没有更好的线程设计可以做到这一点?
目前,如果没有 printMsg,程序将在 5、10、15 等的 1/10 秒内打印多个“hello”。
class Timer {
private int count = 0;
private int N;
private String msg;
private boolean printMsg = false;
public Timer(String s, int N) {
msg = s;
this.N = N;
}
public synchronized void printMsg() throws InterruptedException{
while (count % N != 0 || !printMsg)
wait();
System.out.print(msg + " ");
printMsg = false;
}
public synchronized void printTime() {
printMsg = true;
System.out.print(count + " ");
count ++;
notifyAll();
}
public static void main(String[] args) {
Timer t = new Timer("hello", 5);
new TimerThread(t).start();
new MsgThread(t).start();
}
}
class TimerThread extends Thread {
private Timer t;
public TimerThread(Timer s) {t = s;}
public void run() {
try {
for(;;) {
t.printTime();
sleep(100);
}
} catch (InterruptedException e) {
return;
}
}
}
class MsgThread extends Thread {
private Timer t;
public MsgThread(Timer s) {t = s;}
public void run() {
try {
for(;;) {
t.printMsg();
}
} catch (InterruptedException e) {
return;
}
}
}
最佳答案
不需要使用 printMsg 标记,当 count % N == 0
时,只需使用 notifyAll
即可
public synchronized void printMsg() throws InterruptedException {
wait();
System.out.print(msg + " ");
}
public synchronized void printTime() {
System.out.print(count + " ");
count++;
if (count % N == 0){
notifyAll();
}
}
关于java - 使用 notifyAll() 的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34349811/
我的程序应该使用线程(为了学习线程)按顺序打印出从 1 到 10 的数字。问题是程序陷入僵局。这是为什么? 我像这样创建 10 个线程: for (int i = 0; i < 10; i++) {
这是我的代码,每次运行代码时输出都会有所不同。有时,所有三个读者都会收到通知,输出为: 等待计算... 等待计算... 等待计算... 完成 总计:4950Thread-1 总计:4950Thread
我正在编写一个小Java程序,我需要在其中创建线程(代码中的哲学家),而这些哲学家需要在思考、饥饿和进食之间改变状态。我对这个项目还没有那么深入,我有下一个问题: public class NewMa
这是来自线程的代码块: synchronized(lock) { lock.notifyAll(); System.out.println("waking other threads
假设我有一个用 Java 实现的读/写监视器。 多个读取器或一个写入器可以同时访问数据库(不能同时访问) class RWmonitor{ private int readers = 0;
我正在尝试使用线程在 Java 中实现 Bully 算法。 这是我写的代码。 package newbully; public class NewBully { public static v
我想我应该说的第一件事是我不是在寻找解决方案,这是 hwk,但它运行正确,对我有很大帮助的是澄清.. 我们刚刚在我的面向对象编程类(class)中介绍了线程,并收到了我完成的作业。在我的代码中,我从不
在下面的代码中调用了 notifyAll() 但没有重新激活其他线程。我得到的输出是 beta 等待通知时间:1441870698303,activeWriters:1 alpha 等待通知时间:14
此代码从两个不同的线程打印偶数/奇数。在这里,我的程序卡在 wait() 中,无法使用 notifyAll() 唤醒 sleep 线程。 想知道为什么notifyAll无法唤醒所有 hibernate
所以,我有以下对象(为示例而简化): public class SomeListener implements EventListener{ public final Object lock
我正在做一个练习,模拟商店中具有多个线程的等待队列。 我有一个等候名单和 2 个柜台当某个客户端排在等待列表的第一位并且柜台有空闲时,该客户端进入柜台等待2秒,通知其他客户端然后离开。 我不明白为什么
我不理解 Java 并发实践书中的以下片段: 当只有一个线程可以取得进展时使用 notifyAll 是低效的 - 有时是一点点,有时是非常低效。如果有 10 个线程在一个条件队列上等待,调用 noti
public class ShareResource { private int n = 0; public synchronized void p() throws InterruptedExcep
import java.math.BigInteger; class Numbers { final static int NUMBER = 2; final static int P
我已经使用 JVMTI 实现了一个简单的分析器来显示对 wait() 和 notifyAll() 的调用。作为测试用例,我正在使用。 producer consumer example of Orac
我想知道在这种情况下会发生什么:我有 10 个线程在某些 lockObject 上等待(使用 wait)和 1 个线程(我们称之为 X 线程)试图进入由 lockObject 同步的 block 。那
我有一个有两种方法的类,一个发送消息,另一个确认消息已被接收/处理 public void send(OTAHotelAvailRS otaHotelAvailRS) throws Except
这是我编写的多线程队列的小片段, synchronized void add(int i) { if (count == size) { System.out.println(
所有线程都可以等待,但只能通知1个线程(最后一个线程)。如何notifyAll所有线程? public class Server { static Socket clientSocket; sta
我正在开发一个Java应用程序,当按下GUI中的按钮时,用户可以单击屏幕上的任意位置来记录他们单击的位置的x和y坐标。 这是通过以下方式完成的在整个屏幕上放置一个未修饰的、部分透明的 JFrame,用
我是一名优秀的程序员,十分优秀!