在 Java 中等待条件 [英] Waiting for condition in Java

查看:46
本文介绍了在 Java 中等待条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个线程,当队列为空时将值放入队列并等待这种情况,而它不是.这是我尝试使用的代码,但它会打印

添加新的取值 1取值 2取值 3取值 4

所以它只工作了一次.有什么问题?

import java.util.concurrent.BlockingQueue;导入 java.util.concurrent.LinkedBlockingQueue;公共类 SO {公共字符串测试;public String[] list = new String[] {test};公共静态无效主(字符串 [] args){新 SO();}公共 SO() {去();}BlockingQueueqq = new LinkedBlockingQueue();class Producer 实现 Runnable {公共无效运行(){尝试 {而(真){同步(这个){而 (qq.size() > 0)等待();System.out.println("新增");qq.put("值1");qq.put("值2");qq.put("值3");qq.put("值4");}}} catch (InterruptedException ex) {}}}类消费者实现 Runnable {公共无效运行(){尝试 {而(真){System.out.println("取" + qq.take()+"."+String.valueOf(qq.size())+" left");线程睡眠(1000);}} catch (InterruptedException ex) {}}}公共无效去(){生产者 p = 新生产者();消费者 c = 新消费者();新线程(p).开始();新线程(c).开始();}}

解决方案

wait() 将永远持续,因为你从不调用 notify().

您可以在队列中等待并在您希望等待的线程唤醒时调用通知.为此,您需要将 Producer 更改为:

<前>同步(QQ){而 (qq.size() > 0)qq.wait();System.out.println("新增");qq.put("值1");qq.put("值2");qq.put("值3");qq.put("值4");}

并将消费者更改为读取:

<前>而(真){同步(QQ){System.out.println("取" + qq.take() + "." + String.valueOf(qq.size()) + " left");QQ.通知();}线程睡眠(1000);}

正如史蒂夫在他的回答中所说,您也可以在消费者线程中使用 wait() 以便它可以等到列表中有内容而不是睡眠.所以你的代码会变成:

<前>导入 java.util.concurrent.BlockingQueue;导入 java.util.concurrent.LinkedBlockingQueue;公共类 SO {公共字符串测试;public String[] list = new String[] { test };公共静态无效主(字符串 [] args){新 SO();}公共 SO() {去();}BlockingQueue qq = new LinkedBlockingQueue();class Producer 实现 Runnable {公共无效运行(){尝试 {而(真){同步(QQ){如果 (!qq.isEmpty()) {qq.wait();}System.out.println("新增");qq.put("值1");qq.put("值2");qq.put("值3");qq.put("值4");QQ.通知();}}} catch (InterruptedException ex) {}}}类消费者实现 Runnable {公共无效运行(){尝试 {而(真){同步(QQ){System.out.println("取" + qq.take() + "."+ String.valueOf(qq.size()) + "left");如果(qq.isEmpty()){QQ.通知();qq.wait();}}}} catch (InterruptedException ex) {}}}公共无效去(){生产者 p = 新生产者();消费者 c = 新消费者();新线程(p).开始();新线程(c).开始();}}

I want to make one thread that puts values to a queue when it gets empty and wait for this condition while it is not. Here's the code I've tried to use, but it prints

Adding new
Taking Value 1
Taking Value 2
Taking Value 3
Taking Value 4

So it is working only one time. What is the problem?

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


public class SO {
    public String test;
    public String[] list = new String[] {test};

    public static void main(String[] args) {
        new SO();
    }

    public SO() {
        go();
    }

    BlockingQueue<String> qq = new LinkedBlockingQueue<String>();

    class Producer implements Runnable {
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        while (qq.size() > 0)
                            wait();

                        System.out.println("Adding new");
                        qq.put("Value 1");
                        qq.put("Value 2");
                        qq.put("Value 3");
                        qq.put("Value 4");
                    }
                }
            } catch (InterruptedException ex) {}
        }
    }

    class Consumer implements Runnable {
        public void run() {
            try {
                while(true) {
                    System.out.println("Taking " + qq.take()+". "+String.valueOf(qq.size())+" left");
                    Thread.sleep(1000);
                }
            } catch (InterruptedException ex) {}
        }
    }

    public void go() {
        Producer p = new Producer();
        Consumer c = new Consumer();

        new Thread(p).start();
        new Thread(c).start();
    }
}

解决方案

The wait() will continue forever because you never call notify().

You could wait on the queue and call notify on that when you want the waiting thread to wakeup. To do this you would change Producer to read:

    synchronized (qq) {
        while (qq.size() > 0)
            qq.wait();

            System.out.println("Adding new");
            qq.put("Value 1");
            qq.put("Value 2");
            qq.put("Value 3");
            qq.put("Value 4");
    }

And change Consumer to read:

    while(true) {
        synchronized (qq) {
            System.out.println("Taking " + qq.take() + ". " + String.valueOf(qq.size()) + " left");
            qq.notify();
        }
        Thread.sleep(1000);
    }

As Steve says in his answer you could also use wait() in the consumer thread so it can wait until there is something in the list rather than sleeping. So your code would become:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class SO {
    public String test;
    public String[] list = new String[] { test };

    public static void main(String[] args) {
        new SO();
    }

    public SO() {
        go();
    }

    BlockingQueue qq = new LinkedBlockingQueue();

    class Producer implements Runnable {
        public void run() {
            try {
                while (true) {
                    synchronized (qq) {
                        if (!qq.isEmpty()) {
                            qq.wait();
                        }

                        System.out.println("Adding new");
                        qq.put("Value 1");
                        qq.put("Value 2");
                        qq.put("Value 3");
                        qq.put("Value 4");
                        qq.notify();
                    }
                }
            } catch (InterruptedException ex) {
            }
        }
    }

    class Consumer implements Runnable {
        public void run() {
            try {
                while (true) {
                    synchronized (qq) {
                        System.out.println("Taking " + qq.take() + ". "
                                + String.valueOf(qq.size()) + " left");
                        if (qq.isEmpty()) {
                            qq.notify();
                            qq.wait();
                        }
                    }
                }
            } catch (InterruptedException ex) {
            }
        }
    }

    public void go() {
        Producer p = new Producer();
        Consumer c = new Consumer();

        new Thread(p).start();
        new Thread(c).start();
    }
}

这篇关于在 Java 中等待条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆