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

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

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.

  1. public String getInput() {
  2. if (inputList.isEmpty()) return null;
  3. String input = inputList.get(0);
  4. inputList.remove(0);
  5. return input;
  6. }

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:

  1. public class Queue {
  2. BlockingDeque<String> inputList = new LinkedBlockingDeque<>();
  3. public String getInput() {
  4. try {
  5. System.out.println("waiting on queue");
  6. String input = inputList.takeFirst();
  7. System.out.println("taken " + input);
  8. return input;
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. return null;
  12. }
  13. }
  14. public static void main(String[] args) {
  15. Queue queue = new Queue();
  16. new Thread(() -> {
  17. try {
  18. Thread.sleep(4000);
  19. queue.inputList.add("string");
  20. System.out.println("added string");
  21. Thread.sleep(2000);
  22. queue.inputList.add("string1");
  23. System.out.println("added string 1");
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. }).start();
  28. for (int i = 0; i < 2; i++){
  29. queue.getInput();
  30. }
  31. }
  32. }
英文:

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:

  1. public class Queue {
  2. BlockingDeque&lt;String&gt; inputList = new LinkedBlockingDeque&lt;&gt;();
  3. public String getInput() {
  4. try {
  5. System.out.println(&quot;waiting on queue&quot;);
  6. String input = inputList.takeFirst();
  7. System.out.println(&quot;taken &quot; + input);
  8. return input;
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. return null;
  12. }
  13. }
  14. public static void main(String[] args) {
  15. Queue queue = new Queue();
  16. new Thread(() -&gt; {
  17. try {
  18. Thread.sleep(4000);
  19. queue.inputList.add(&quot;string&quot;);
  20. System.out.println(&quot;added string&quot;);
  21. Thread.sleep(2000);
  22. queue.inputList.add(&quot;string1&quot;);
  23. System.out.println(&quot;added string 1&quot;);
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. }).start();
  28. for (int i = 0; i &lt; 2; i++){
  29. queue.getInput();
  30. }
  31. }
  32. }

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:

确定