Results 1 to 5 of 5
Thread: ArrayBlockingQueue
- 04-25-2011, 09:08 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
ArrayBlockingQueue
Very simple program. I'm using ArrayBlockingQueue to synchronize two methods -- putting and pulling into/from the array with dimension of 1. Adding takes 0.1 seconds more than usually (I did it specially, for process to be muuuch longer than pulling) bu because I'm using ArrayBlockingQueue as a container (variable), output should contain number ("0"). But it's NULL, as if addition was ignored :S What can i do?
Java Code:import java.util.concurrent.ArrayBlockingQueue; public class threads { public static void main(String[] args) { Queue queue = new Queue(); Add add = new Add(queue.a); Show show = new Show(queue.a); add.start(); show.start(); } } class Add extends Thread { private ArrayBlockingQueue<Integer> a; public Add(ArrayBlockingQueue<Integer> array) { a=array; } public void run() { for(int k=0;k<a.size();k++) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } try { a.put(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Show extends Thread { private ArrayBlockingQueue<Integer> a; public Show(ArrayBlockingQueue<Integer> array) { a=array; } public void run() { System.out.println(a.poll()); } } class Queue { public ArrayBlockingQueue<Integer> a = new ArrayBlockingQueue<Integer>(1); }
- 04-25-2011, 09:27 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You start your 'add' and 'show' Threads one after another; your 'add' Thread first sleeps a while before it adds an entry to your queue while your 'show' Thread immediately polls the queue; it is still empty of course because the 'add' Thread is still taking its nap. Use 'take' instead of 'poll' to make your 'show' Thread wait until there actually is something present in that queue.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2011, 10:09 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0
I'm still getting the wrong answer :S
Java Code:import java.util.concurrent.ArrayBlockingQueue; public class threads { public static void main(String[] args) throws InterruptedException { Queue queue = new Queue(); Add add = new Add(queue.a); Show show = new Show(queue.a); add.start(); show.start(); } } class Add extends Thread { private ArrayBlockingQueue<Integer> a; public Add(ArrayBlockingQueue<Integer> array) { a=array; } public void run() { for(int k=0;k<a.size();k++) { try {a.put(new Integer(10));} catch (InterruptedException e) {e.printStackTrace();} } } } class Show extends Thread { private ArrayBlockingQueue<Integer> a; public Show(ArrayBlockingQueue<Integer> array) { a=array; } public void run() { try {Thread.currentThread().sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} System.out.println(a.poll()); } } class Queue { public ArrayBlockingQueue<Integer> a = new ArrayBlockingQueue<Integer>(1); }
- 04-25-2011, 10:23 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
What do you think the size() of that Collection will be when your 'add' Thread starts running and nothing is in that queue yet?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-25-2011, 10:30 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 45
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks