使用java.util.concurrent阻止操作直到列表变为非空。

huangapple go评论82阅读模式
英文:

Block actions till list becomes non-empty with java.util.concurrent

问题

我需要你的帮助。我应该在我的练习中使用java.util.concurrent包,但我不知道该如何做。问题仅涉及get方法。

如何编写代码以等待给定的列表(变量:inputList)变为非空?

问候

英文:

I need your help. I should use the java.util.concurrent package in my exercise but I don't know how to do it. The question is only about the get method.

public String getInput() {
        if (inputList.isEmpty()) return null;
        String input = inputList.get(0);
        inputList.remove(0);
        return input;
}

How do I need to write the code to wait till the given list (variable: inputList) becomes non-empty?

Greetings

答案1

得分: 0

you could try using the LinkedBlockingDeque class from the java.util.concurrent
package which implements the BlockingDequeinterface.

it lets you add items to the BlockingDeque and the take* methods block until there is an element available and remove it after fetching. Have a look at the Javadoc

Here is an example:

public class Queue {

    BlockingDeque<String> inputList = new LinkedBlockingDeque<>();

    public String getInput() {
        try {
            System.out.println("waiting on queue");
            String input = inputList.takeFirst();
            System.out.println("taken " + input);
            return input;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

        Queue queue = new Queue();

        new Thread(() -> {
            try {
                Thread.sleep(4000);
                queue.inputList.add("string");
                System.out.println("added string");
                Thread.sleep(2000);
                queue.inputList.add("string1");
                System.out.println("added string 1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        for (int i = 0; i < 2; i++){
            queue.getInput();
        }
    }

}
英文:

you could try using the LinkedBlockingDeque class from the java.util.concurrent
package which implements the BlockingDequeinterface.

it lets you add items to the BlockingDeque and the take* methods block until there is an element available and remove it after fetching. Have a look at the Javadoc

Here is an example:

public class Queue {

    BlockingDeque&lt;String&gt; inputList = new LinkedBlockingDeque&lt;&gt;();

    public String getInput() {
        try {
            System.out.println(&quot;waiting on queue&quot;);
            String input = inputList.takeFirst();
            System.out.println(&quot;taken &quot; + input);
            return input;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

        Queue queue = new Queue();

        new Thread(() -&gt; {
            try {
                Thread.sleep(4000);
                queue.inputList.add(&quot;string&quot;);
                System.out.println(&quot;added string&quot;);
                Thread.sleep(2000);
                queue.inputList.add(&quot;string1&quot;);
                System.out.println(&quot;added string 1&quot;);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        for (int i = 0; i &lt; 2; i++){
            queue.getInput();
        }
    }

}

huangapple
  • 本文由 发表于 2020年4月7日 16:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61076157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定