Producer-Consumer modelling issues
/** * Using blocking queues to implement producer-consumer models * Blocking queues only allows elements to be accessed in a FIFO * @author Bingyue * */public class Producercustomerpattern {public static V OID Main (string[] args) {//producer and consumer shared storage area blockingqueue<integer> blockqueue=new linkedblockingqueue ();/** * Here the external class accesses the inner class of the static method call, and the external class instance must be created first. * If you don't need a connection between an inner class object and its enclosing class object, you can declare the inner class as static. This is commonly referred to as a static nested class (Nested Classes). * To understand the meaning of static when applied to internal classes, you must remember that a normal inner class object implicitly holds a reference to the outer class object that created it. * However, this is not the case when the inner class is static. A nested class means: * 1. To create an object of a nested class, you do not need an object of its enclosing class. * 2. Non-static perimeter class objects cannot be accessed from objects in the nested class. */producercustomerpattern ps=new Producercustomerpattern ();//Note the way the inner class is created thread Pro=new thread (ps.new Producer ( Blockqueue)); Thread cus=new Thread (ps.new Customer (blockqueue));p Ro.start (); Cus.start ();} Class Producer implements Runnable{private final blockingqueue<integer> queue;public Producer (blockingqueue< Integer> queue) {this.queue=queue;} @Overridepublic void Run () {for (int i=0;i<=10;i++) {try {System.out.println ("produced:" +i); Queue.put (i);} catch (Interruptedexception e) {//for blocked insertionsAnd remove methods both put and take are thrown interruptedexceptione.printstacktrace ();}}} /** * Created by implementing the Runnable interface thread * (1). Defines a class that implements the Runnable interface, overriding the run () method in the interface. Add specific task code or processing logic to the run () method. * (2). Create an object of the Runnable interface implementation class. * (3). Creating an object of the thread class requires encapsulating the object of the preceding Runnable interface implementation class. (Interfaces can implement multiple inheritance) * (4). Call the Start () method of the thread object to start the thread * * (1) by inheriting the thread class. First define a class to inherit the thread parent class, overriding the run () method in the parent class. Add specific task code or processing logic to the run () method. * (2). Create an object of the ThreadDemo2 class directly, or you can take advantage of polymorphism, where the variable is declared as the type of the parent class. * (3). Call the Start method, thread T starts, implicitly calls the run () method. */class Customer implements Runnable{private final blockingqueue<integer> queue;public customer (blockingqueue <Integer> queue) {this.queue=queue;} @Overridepublic void Run () {while (true) {try {System.out.println ("customed:" +queue.take ());} catch (Interruptedexception e) {e.printstacktrace ();}}}}
Implement producer-consumer models with blocking queues