| Refresh | Home EGTry.com

provider send jobs through blocking queue - shutdown the queue gracefully through poison objects


java code

/*
 * Copyright@ 2011 www.egtry.com
 */

package egtry.thread;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
public class ArrayBlockingQueueTest {
	private static ArrayBlockingQueue<Job> queue;
	private static int capacity=20;
	private static int Nconsumer=10;
	private static int Njob=30;
  public static void main(String[] args) {
	  boolean isFIFO=true;
	  queue=new ArrayBlockingQueue<Job>(capacity,isFIFO);
  
	  Provider provider=new Provider();
	  provider.start();
	  
	  for(int i=0; i<Nconsumer; i++) {
		  Consumer consumer=new Consumer();
		  consumer.start();
	  }

  }
  
  
  public static class Job {
	  public String id;
	  
	  public Job(String id) {
		  this.id=id;
	  }
	  
	  public boolean isDone() {
		  return id==null;
	  }
  }
  
  public static class Provider extends Thread {
	  
	  @Override
	  public void run() {
		  try {
		  for(int i=0; i<Njob; i++) {
			  Job job=new Job(i+"");
			  System.out.println("send job "+job.id+" at: "+new Date());			  
			  queue.put(job);
		  }
		  
		  Job lastJob=new Job(null);
		  int n=Nconsumer+capacity;
		  for(int i=0; i<n; i++) {
			  queue.put(lastJob);
		  }
		 
		  } catch (InterruptedException e) {
			  System.out.println("Provider thread "+this.getId()+" interrupted");
			  return;
		  }
	  }
  }
  
  public static class Consumer extends Thread {
	  @Override
	  public void run() {
		  while(true) {
			  Job job;
			  try {
				  Thread.sleep(1000);
				  job=queue.take();
				  System.out.println("Got the job: "+job.id);
				  if (job.isDone()) {
					  return;
				  }
			  } catch (InterruptedException e) {
				  System.out.println("Consumer thread "+this.getId()+" interrupted");
				  return;
			  }
		  }
	  }
  }
  
}


Output

send job 0 at: Mon Oct 24 11:20:34 CDT 2011
send job 1 at: Mon Oct 24 11:20:34 CDT 2011
send job 2 at: Mon Oct 24 11:20:34 CDT 2011
send job 3 at: Mon Oct 24 11:20:34 CDT 2011
send job 4 at: Mon Oct 24 11:20:34 CDT 2011
send job 5 at: Mon Oct 24 11:20:34 CDT 2011
send job 6 at: Mon Oct 24 11:20:34 CDT 2011
send job 7 at: Mon Oct 24 11:20:34 CDT 2011
send job 8 at: Mon Oct 24 11:20:34 CDT 2011
send job 9 at: Mon Oct 24 11:20:34 CDT 2011
send job 10 at: Mon Oct 24 11:20:34 CDT 2011
send job 11 at: Mon Oct 24 11:20:34 CDT 2011
send job 12 at: Mon Oct 24 11:20:34 CDT 2011
send job 13 at: Mon Oct 24 11:20:34 CDT 2011
send job 14 at: Mon Oct 24 11:20:34 CDT 2011
send job 15 at: Mon Oct 24 11:20:34 CDT 2011
send job 16 at: Mon Oct 24 11:20:34 CDT 2011
send job 17 at: Mon Oct 24 11:20:34 CDT 2011
send job 18 at: Mon Oct 24 11:20:34 CDT 2011
send job 19 at: Mon Oct 24 11:20:34 CDT 2011
send job 20 at: Mon Oct 24 11:20:34 CDT 2011
Got the job: 0
send job 21 at: Mon Oct 24 11:20:35 CDT 2011
Got the job: 2
send job 22 at: Mon Oct 24 11:20:35 CDT 2011
Got the job: 3
Got the job: 5
Got the job: 6
Got the job: 7
Got the job: 9
send job 23 at: Mon Oct 24 11:20:35 CDT 2011
send job 24 at: Mon Oct 24 11:20:35 CDT 2011
Got the job: 4
send job 25 at: Mon Oct 24 11:20:35 CDT 2011
send job 26 at: Mon Oct 24 11:20:35 CDT 2011
send job 27 at: Mon Oct 24 11:20:35 CDT 2011
send job 28 at: Mon Oct 24 11:20:35 CDT 2011
send job 29 at: Mon Oct 24 11:20:35 CDT 2011
Got the job: 8
Got the job: 1
Got the job: 10
Got the job: 11
Got the job: 12
Got the job: 13
Got the job: 14
Got the job: 16
Got the job: 15
Got the job: 17
Got the job: 18
Got the job: 19
Got the job: 20
Got the job: 21
Got the job: 22
Got the job: 23
Got the job: 24
Got the job: 25
Got the job: 26
Got the job: 27
Got the job: 28
Got the job: 29
Got the job: null
Got the job: null
Got the job: null
Got the job: null
Got the job: null
Got the job: null
Got the job: null
Got the job: null
Got the job: null
Got the job: null