Asdfasf

Sunday, October 14, 2012

Simple Producer Consumer With LinkedBlockingQueue

Here is a simple Producer/Consumer sample using java.util.concurrent.LinkedBlockingQueue. Simply, Cachiers process tasks waiting in dropbox produced by Producer.

Task class which is processed by Cachier:

 public class Task {  
      private int taskNumber;  
      public Task(int taskNumber) {  
           this.setTaskNumber(taskNumber);  
      }  
      public void setTaskNumber(int taskNumber) {  
           this.taskNumber = taskNumber;  
      }  
      public int getTaskNumber() {  
           return taskNumber;  
      }  
 }  

Dropbox class is the box that tasks are dropped in. This class has LinkedBlockingQueue containing task objects to be processed with max size 20


 import java.util.concurrent.LinkedBlockingQueue;  
 public class Dropbox {  
      LinkedBlockingQueue<Task> blockingQueue = new LinkedBlockingQueue<Task>(20);  
      public Task take() throws InterruptedException {  
           return blockingQueue.take();  
      }  
      public void put(Task t) throws InterruptedException {  
           blockingQueue.put(t);  
      }  
 }  

Producer is responsible from producing tasks and putting them to DropBox. If dropbox reaches its limit, producer thread will be blocked till queue has empty slot


 public class Producer implements Runnable {  
      private Dropbox d;  
      public Producer(Dropbox d) {  
           this.d = d;  
      }  
      @Override  
      public void run() {  
           int i = 0;  
           while (true) {  
                Task t = new Task(i++);                 
                try {  
                     d.put(t);  
                     System.out.println(t.getTaskNumber() + " is added");  
                } catch (InterruptedException e) {  
                     e.printStackTrace();  
                }  
           }  
      }  
 }  

Cachier is consumer and responsible from processing tasks, it waits till dropbox has a task to be processed

 public class Cashier implements Runnable {  
      private Dropbox d;  
      private String name;  
      public Cashier(Dropbox d, String name) {  
           this.d = d;  
           this.name = name;  
      }  
      @Override  
      public void run() {  
           while (true) {  
                try {  
                     Task t = d.take();  
                     System.out.println("Processing " + t.getTaskNumber() + " by "  
                               + name);  
                     Thread.sleep(1000);  
                } catch (InterruptedException e1) {  
                     // TODO Auto-generated catch block  
                     e1.printStackTrace();  
                }  
           }  
      }  
 }  

Here is the main class starting application with 1 producer and 3 consumer

 public class Main {  
      public static void main(String[] args) {  
           Dropbox d = new Dropbox();  
           Producer p = new Producer(d);  
           Cashier c1 = new Cashier(d, "Ahmet");  
           Cashier c2 = new Cashier(d, "Mehmet");  
           Cashier c3 = new Cashier(d, "Fatih");  
           new Thread(c1).start();  
           new Thread(c2).start();  
           new Thread(c3).start();  
           new Thread(p).start();  
      }  
 }  

No comments: