Results 1 to 5 of 5
  1. #1
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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);
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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);
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •