Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java producer consumer queue

// Java Program to demonstrate producer consumer
// problem solution
  
// Import the BlockingQueue interface and
// ArrayBlockingQueue class
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
  
// Create a Main class for execution
public class Main {
    public static void main(String[] args)
    {
  
        // Create an ArrayBlockingQueue object with capacity
        // 4
        BlockingQueue<Integer> bqueue
            = new ArrayBlockingQueue<Integer>(4);
  
        // Create 1 object each for producer
        // and consumer and pass them the common
        // buffer created above
        producer p1 = new producer(bqueue);
        consumer c1 = new consumer(bqueue);
  
        // Create 1 thread each for producer
        // and consumer and pass them their
        // respective objects.
        Thread pThread = new Thread(p1);
        Thread cThread = new Thread(c1);
  
        // Start both threads
        pThread.start();
        cThread.start();
    }
}
  
class producer implements Runnable {
  
    BlockingQueue<Integer> obj;
  
    public producer(BlockingQueue<Integer> obj)
    {
        this.obj = obj;
    }
  
    @Override public void run()
    {
        for (int i = 1; i <= 4; i++) {
            try {
                obj.put(i);
                System.out.println("Produced " + i);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  
class consumer implements Runnable {
  
    BlockingQueue<Integer> obj;
  
    int taken = -1;
  
    public consumer(BlockingQueue<Integer> obj)
    {
        this.obj = obj;
    }
  
    @Override public void run()
    {
        while (taken != 4) {
            try {
                taken = obj.take();
                System.out.println("Consumed " + taken);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: ways to add properties to Bean Spring 
Java :: Bad JNI version returned from JNI_OnLoad in 
Java :: java codigo para criar um aleatorio entre valores 
Java :: throws multiple exception 
Java :: discount computation in java 
Java :: spigot check if inventory is empty 
Java :: hystrix configuration spring boot 
Java :: plot vector field in java 
Java :: expiry time of otp android 
Java :: Using UUID spring boot Neo4J 
Java :: initialize generic array java 
Java :: java program scan folder find files using time 
Java :: How to Register a Custom Auto-Configuration? 
Java :: How to center a print statement text? 
Java :: calling a method after the build method is run 
Java :: what is the import for gogga class java 
Java :: Reference in java equal operator 
Java :: infinite loop in java 
Java :: how much epsom salt should strawberries need 
Java :: java unused import statement 
Java :: java parse date with optional timezone 
Java :: how to install java jdk 8 on ubuntu 20.04 for spark 
Java :: java assertions 
Java :: binomial heap implementation java 
Java :: shared preferences saved value unsaved in android 
Java :: java escribir ventana grafica 
Java :: android studio cannot resolve @nullable 
Java :: java.lang.IllegalMonitorStateException: object not locked by thread before wait() 
Java :: How to disable special characters on keyboard in android 
Java :: what is abstract class 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =