简体   繁体   English

阻止异步队列Java

[英]Blocking async queues Java

I'm trying to figure out a way to implement the following in Java. 我试图找出一种在Java中实现以下内容的方法。

Thread1 will add jobs to queue1. 线程1将作业添加到队列1。 Another different thread (Thread2) will add jobs to queue2. 另一个不同的线程(Thread2)将作业添加到queue2。

In the run() method of Thread1 I wait until there's a job in queue 1, and let's say I will print it, if and only if there are no awaiting jobs in queue2. 在Thread1的run()方法中,我等待直到队列1中有一个作业,并且说,并且仅当队列2中没有等待的作业时,我才打印它。

How may I notify Thread1 that Thread2 has added a job in queue2? 如何通知Thread1 Thread2已在queue2中添加了作业? Here is Thread1 Class 这是Thread1类

public class Thread1 implements Runnable {
private List queue1 = new LinkedList();

public void processData(byte [] data, int count) {
    byte[] dataCopy = new byte[count];
    System.arraycopy(data, 0, dataCopy, 0, count);
    synchronized(queue1) {
        queue1.add(data);
        queue1.notify();
    }
}

public void run() {
    byte [] data;

    while(true) {
        // Wait for data to become available
        synchronized(queue1) {
            while(queue1.isEmpty()) {
                try {
                    queue1.wait();
                } catch (InterruptedException e) {}
            }
            data = (byte[]) queue1.remove(0);
        }
        // print data only if queue2 has no awaiting jobs in it
    }
}

You have not quite well explained your question and I am not sure what you are trying to ask -its very confusing to read what you have written. 您还没有很好地解释您的问题,我不确定您要问的是什么-阅读您写的内容会非常令人困惑。 Also, I don't see any code for Thread-2 and Queue-2 . 另外,我看不到Thread-2Queue-2任何代码。

So I am going to put general advice, 因此,我将提出一般性建议,

1. Use existing implementation of Blocking Queue instead of doing private List queue1 = new LinkedList(); 1. 使用现有的“阻塞队列”实现,而不是执行private List queue1 = new LinkedList(); and then doing synchronized(queue1) . 然后做synchronized(queue1)

Here is documentation of BlockingQueue interface. 这是BlockingQueue接口的文档 You can use class , LinkedBlockingQueue as implementation. 您可以使用LinkedBlockingQueue类作为实现。

2. Sample code - If you browse above link of BlockingQueue documentation, you see code at the bottom highlighting as how to write consumers and producers. 2. 示例代码 -如果浏览上方BlockingQueue文档的链接,则会在底部突出显示的代码中看到如何编写使用者和生产者。 There you don't see instance of queue getting created inside Thread class but set via constructor - that way you can share a single queue with as many threads as you like - by passing reference to queue in Runnable constructor. 在那里,您看不到在Thread类中创建队列的实例,而是通过构造函数设置的-通过将引用传递给Runnable构造函数,您可以与任意多个线程共享一个队列。

3. BlockingQueue implementations are thread-safe - so you don't have to synchronize on queue instances. 3. BlockingQueue实现是线程安全的 -因此您不必在队列实例上进行synchronize You can freely pass queue instances to as many threads as you like believing that its methods will be called in synchronized way. 您可以自由地将队列实例传递给尽可能多的线程,因为他们认为将以同步方式调用其实例。

So I suggest that you try to rewrite whatever program you are trying to write using above construct and code samples and come back for any more questions. 因此,我建议您尝试使用上述构造和代码示例重写要尝试编写的任何程序,然后再提出任何其他问题。

Hope it helps !! 希望能帮助到你 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM