- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序运行并正常等待,但没有到达通知。
public static void main(String[] args) {
LinkedBlockingQueue<Can> beltQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Can> sterilizeQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Can> sterilize1Q = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Can> fillingQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Can> sealingQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Can> labellingQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Box> packagingQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Bigbox> boxingQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Bigbox> loadingQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Bigbox> deliveryQ = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Bigbox> customer = new LinkedBlockingQueue<>();
Belt b = new Belt(beltQ, boxingQ);
Sterilize s = new Sterilize(beltQ, sterilizeQ, boxingQ);
Sterilize1 s1 = new Sterilize1(sterilizeQ, sterilize1Q, boxingQ);
Filling f = new Filling(sterilize1Q, fillingQ, boxingQ);
Sealing seal = new Sealing(fillingQ, sealingQ, boxingQ);
Labelling l = new Labelling(sealingQ, labellingQ, boxingQ);
Packaging p = new Packaging(labellingQ, packagingQ, boxingQ);
Boxing box = new Boxing(packagingQ, boxingQ);
b.start();
s.start();
s1.start();
f.start();
seal.start();
l.start();
p.start();
box.start();
for (int i = 1; i < 4; i++) {
ForkliftTruck ft = new ForkliftTruck(boxingQ, loadingQ, i);
ft.start();
}
LoadingArea la = new LoadingArea(2);
for (int i = 1; i < 4; i++) {
DeliveryVan dv = new DeliveryVan(la, i, loadingQ, deliveryQ, customer);
dv.start();
}
}
class Packaging extends Thread {
LinkedBlockingQueue<Can> labellingQ;
LinkedBlockingQueue<Box> packagingQ;
LinkedBlockingQueue<Bigbox> boxingQ;
public Packaging(LinkedBlockingQueue<Can> labellingQ, LinkedBlockingQueue<Box> packagingQ, LinkedBlockingQueue<Bigbox> boxingQ) {
this.labellingQ = labellingQ;
this.packagingQ = packagingQ;
this.boxingQ = boxingQ;
}
synchronized public void run() {
int boxNo = 1;
while (true) {
try {
synchronized (boxingQLock) {
while (boxingQ.size() == 1) {
System.out.println("Production is paused");
boxingQLock.wait();
}
}
if (labellingQ.size() == 6) {
System.out.println("Packaging cans into box ");
Can can1 = labellingQ.take();
Can can2 = labellingQ.take();
Can can3 = labellingQ.take();
Can can4 = labellingQ.take();
Can can5 = labellingQ.take();
Can can6 = labellingQ.take();
System.out.println("Cans have been shrink wrapped");
Box bx = new Box(can1, can2, can3, can4, can5, can6, boxNo);
packagingQ.put(bx);
System.out.println("Cans " + can1.CanNo + " " + can2.CanNo + " " + can3.CanNo + " " + can4.CanNo + " " + can5.CanNo + " " + can6.CanNo + " has been packed into packet " + bx.BoxNo);
boxNo++;
}
} catch (Exception e) {
}
}
}
}
class Boxing extends Thread {
LinkedBlockingQueue<Box> packagingQ;
LinkedBlockingQueue<Bigbox> boxingQ;
public Boxing(LinkedBlockingQueue<Box> packagingQ, LinkedBlockingQueue<Bigbox> boxingQ) {
this.packagingQ = packagingQ;
this.boxingQ = boxingQ;
}
synchronized public void run() {
int bigboxNo = 1;
while (true) {
try {
LinkedBlockingQueue<Box> packet = new LinkedBlockingQueue<Box>(2);
if (boxingQ.size() == 1) {
System.out.println("No space!! Waiting for boxed to be taken!");
System.out.println("No space!! Waiting for boxed to be taken!");
}
for (int i = 1; i < 3; i++) {
Box b1 = packagingQ.take();
System.out.println("Loading packets into boxes");
packet.put(b1);
}
Bigbox big = new Bigbox(packet, bigboxNo);
bigboxNo++;
System.out.println("Packets have been loaded into box " + big.BigboxNo);
boxingQ.put(big);
System.out.println("BoxingQ size: " + boxingQ.size());
System.out.println("Box has been stored into loading area");
} catch (Exception e) {
}
}
}
}
class ForkliftTruck extends Thread {
LinkedBlockingQueue<Bigbox> boxingQ;
LinkedBlockingQueue<Bigbox> loadingQ;
int forkno;
public ForkliftTruck(LinkedBlockingQueue<Bigbox> boxingQ, LinkedBlockingQueue<Bigbox> loadingQ, int forkno) {
this.boxingQ = boxingQ;
this.loadingQ = loadingQ;
this.forkno = forkno;
}
public synchronized void run() {
while (true) {
try {
synchronized (boxingQLock) {
Bigbox bb = boxingQ.take();
boxingQLock.notifyAll();
Thread.sleep(500);
System.out.println("Production is resumed");
System.out.println("Forklift " + forkno + " has picked box " + bb.BigboxNo);
Random rand = new Random();
int fdefect = rand.nextInt(4) + 1;
if (fdefect == 2) {
Thread.sleep(1000);
System.out.println("Forklift " + forkno + " has broken down......waiting for repair");
Thread.sleep(5000);
System.out.println("Forklift " + forkno + " is repaired......successfully placed box " + bb.BigboxNo + " in the loading bay");
loadingQ.put(bb);
System.out.println("LoadingQ size: " + loadingQ.size());
} else {
Thread.sleep(1000);
System.out.println("Forklift " + forkno + " is in good condition");
Thread.sleep(500);
System.out.println("Forklift " + forkno + " has successfully placed box " + bb.BigboxNo + " in the loading bay");
loadingQ.put(bb);
System.out.println("LoadingQ size: " + loadingQ.size());
}
}
} catch (Exception e) {
}
}
}
}
从输出中可以看到,一旦 boxingQ.size
达到 1,它就会暂停,但是一旦从队列中取出并减少到 0,notify()
功能似乎不起作用
这是我的输出
Loading packets into boxes
Cans 8 9 11 12 14 16 has been packed into packet 2 //2 packets go into one box
Packets have been loaded into box 1
BoxingQ size: 1
Box has been stored into loading area
Production is paused
Forklift 1 has picked box 1
如您所见,由于叉车已拾取箱子,boxingQ 目前队列中有 0 个元素。但生产并未恢复
最佳答案
调用notify
将释放等待您调用notify
的对象的线程。也就是说,如果一个线程调用 obj.wait(),另一个线程必须调用 obj.notify() 来唤醒等待线程。
就您而言,看起来您需要等到 boxingQ 满足某个条件。您可以为此使用对象:
static final Object boxingQLock=new Object();
synchronized (boxingQLock) {
while ( boxingQ does not satisfy the condition) {
boxingQLock.wait();
}
// when you come here, boxingQ satisfies the condition, and you are
// holding a lock on it
}
并且,在另一个修改 boxingQ 的线程中:
synchronized (boxingQLock) {
// Modify boxingQ
// Notify a thread
boxingQLock.notify(); //or notifyAll()
}
抛开所有关于等待/通知的讨论:您正在使用BlockingQueue
。这意味着,您可以简单地调用 take
并等待有人将某些内容放入队列,而根本不用担心同步/等待/通知。然而,像下面这样的代码本质上是活泼的:
if (labellingQ.size() == 6) {
System.out.println("Packaging cans into box ");
Can can1 = labellingQ.take();
Can can2 = labellingQ.take();
...
因为如果没有显式锁定,队列大小可能会在您读取后发生变化。无法保证在读取长度后其他线程不会执行相同操作并尝试从中获取元素。一种方法是取 6 次,如果不够就让队列负责等待。或者,使用 labellingQ 锁,检查是否有足够的项目并在同步块(synchronized block)中获取所有 6 个项目,这样当您知道有足够的项目时,其他线程就无法在您之前获取它们。
关于java - 即使参数已经满足,Notify() 也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755711/
我正在尝试在我安装的 Laravel 5.1 中设置 Gulp。我已经运行然后命令 npm install正如 Laravel 文档中所指定的那样,效果很好。 但是,当我现在运行命令时 gulp我收到
我有两个扩展线程的类和一个等待/通知 class A extends Thread { int r = 20; public void run() { try {
我正在尝试在后台服务中启动通知,这也是位置监听器。我有一个功能: public Notification CreateNotification(){ Intent notificati
编辑:我将其范围缩小到 python 代码中 Notifier.notify('Something') 的问题。当 python 脚本从 launchd 启动时,这不会产生预期的行为。我的其他 pyt
我正在尝试使用 bootstrap-notify v3.1.3、typescript、aurelia 和 VS2015 在我的网站上显示警报。 我的 aurelia 组件是: //myAlert.ts
注:other question的标题不同,导致无法识别为匹配的。 系统.类 TCollection = class(TPersistent) protected procedure Notify
我正在将一个项目从 Sprockets 迁移到 Webpacker。 我似乎无法正确运行的最后一件事是通知。 我曾经能够做到:$.notify('Test') 但现在我得到了 Uncaught Typ
我在一个项目中有多个用户控件,其中一个从 XML 中检索项目,创建“ClassItem”类型的对象,并应通知其他用户控件有关这些项目的信息。 我为我的对象创建了一个类(所有项目都将具有的“模型”):
我以为我会理解 Java 中的并发概念,但现在有一件事打破了我的理解: 为什么我必须在同步块(synchronized block)中包含对 wait() 和 notify() 方法的调用? 假设我有
我正在开发 NodeJS/Electron/Angular 应用程序,我正在使用 node-notifier 模块。一切正常,但在我的通知消息底部有一个不合适的“toast”字符串。它仅在我使用“图标
我正在基于Sproutcore 1.9.1的Web应用程序上工作。要从服务器检索数据 发出一个SC.Request.getUrl()请求,该请求在除IE8之外的所有浏览器中都可以正常工作。 对于IE8
我试图写一个关于如何使用wait()和notify()的示例,但是似乎无法通知wait() public class Transfer { private int[] data; pr
我想使用QAudioInput从麦克风捕获声音,对其进行处理然后再播放。据我了解,我需要连接到notify信号和内部处理程序,以便用户使用readAll()函数来获取原始数据。但是问题是,此处理函数永
public class Signal2NoiseRatio { public ImagePlus SingleSNR(ImagePlus imagePlus) throws Interrup
为什么只有第一个任务退出后才调用dispatchGroup.notify? 下面的代码输出如下: 1) Did the other thing **2) Did all the things** 3)
假设线程 T1 正在等待进入同步块(synchronized block),线程 T2 在同步块(synchronized block)内 wait(),并且线程 T3 在同步块(synchroniz
我正在尝试仅使用等待/通知同步来实现合并排序。我知道更高级的结构,例如 Fork/Join、Executors。等等,但我需要在这里使用工作/通知。基于此https://courses.cs.wash
了解 notifyAll 让我对 notify 产生了一些疑问:在典型情况下,我们有多个线程正在等待对 methody notify 的调用。当这种情况发生时,其中一个线程(之前调用了 wait 方法
我正在使用 wait() 和 notify() 编写一个示例程序,但是当调用 notify() 时,会出现多个线程被唤醒而不是一个。 代码是: public class MyQueue { O
根据17.2.4. Interactions of Waits, Notification, and Interruption : Similarly, notifications cannot be
我是一名优秀的程序员,十分优秀!