Results 1 to 2 of 2
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default array blocking queue and an inundation of data

    Below I illustrate the basic use of ArrayBlockingQueue. This is a trivial example. I set the limit to 2 so the producer blocks after two insertions until an item is consumed. Because we are dealing with a basic counter that increments, we do not lose any data. Since after queue is freed up we simply increment by 1, so we are able to store all numbers. However, in real world situation, you might be storing data coming from redis server into the queue. And redis server may be publishing a plethora of data. So when the queue blocks, and new data comes in, what happens? Is the currently blocked data lost forever and the new data that came in is now blocked, or is the new data that came in ignored and the old data remains being blocked until queue is freed up?

    Java Code:
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    
    
    public class BlockingQueueBlocking {
    	private BlockingQueue<Integer> sQueue = new ArrayBlockingQueue<Integer>(2);
    	int counter=0;
    	
    	private void test() throws InterruptedException{
    		Thread t1 = new Thread(new Runnable(){
    			public void run() {
    				produce();
    			}			
    		});
    		
    		Thread t2 = new Thread(new Runnable(){
    			public void run(){
    				consume();
    			}
    		});
    		
    		t1.start();
    		t2.start();
    		
    		t1.join();
    		t2.join();
    	}
    	
    	private void produce(){
    		while(true){
    			try {
    				sQueue.put(counter++);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	private void consume(){
    		Integer value=-1;
    		while(true){
    			try {
    				Thread.sleep(5000);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			try {
    				value = sQueue.take();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			System.out.println("value: " + value);
    		}
    		
    	}
    	
    	public static void main(String[] args) throws InterruptedException {
    		BlockingQueueBlocking test = new BlockingQueueBlocking();
    		test.test();
    	}
    
    }

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

    Default Re: array blocking queue and an inundation of data

    Quote Originally Posted by johnmerlino View Post
    Below I illustrate the basic use of ArrayBlockingQueue. This is a trivial example. I set the limit to 2 so the producer blocks after two insertions until an item is consumed. Because we are dealing with a basic counter that increments, we do not lose any data. Since after queue is freed up we simply increment by 1, so we are able to store all numbers. However, in real world situation, you might be storing data coming from redis server into the queue. And redis server may be publishing a plethora of data. So when the queue blocks, and new data comes in, what happens? Is the currently blocked data lost forever and the new data that came in is now blocked, or is the new data that came in ignored and the old data remains being blocked until queue is freed up?
    No data in the queue can get lost; your producer thread (that gets its data from a server somehow) may be blocked because the queue is full and may not fetch a new data item from the server, but that's a problem 'outside' of the blocking queue.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. My new post: Producer Consumer solution using blocking Queue in java
    By javin.paul in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-22-2012, 11:15 AM
  2. Replies: 0
    Last Post: 05-27-2011, 09:46 AM
  3. Need help with array queue
    By bap2 in forum Advanced Java
    Replies: 3
    Last Post: 11-01-2010, 06:06 PM
  4. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:35 PM
  5. Help with queue array please!!
    By vmcg105 in forum New To Java
    Replies: 1
    Last Post: 03-08-2008, 08:03 PM

Posting Permissions

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