Let's take a look at the code for the following program: Classic producer consumers to implement
Packagecom.bjsxt.height.design016; Public Final classData {PrivateString ID; PrivateString name; PublicData (string ID, string name) { This. ID =ID; This. Name =name; } PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"{ID:" + ID + ", Name:" + name + "}"; } }
Packagecom.bjsxt.height.design016;ImportJava.util.Random;ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.TimeUnit;ImportJava.util.concurrent.atomic.AtomicInteger; Public classProviderImplementsrunnable{//Shared Buffers PrivateBlockingqueue<data>queue; //whether a variable is started between threads, there are features that force the refresh from main memory. Instantly returns the status of a thread Private volatile BooleanIsRunning =true; //ID Generator Private StaticAtomicinteger count =NewAtomicinteger (); //Random Objects Private StaticRandom r =NewRandom (); PublicProvider (blockingqueue queue) { This. Queue =queue; } @Override Public voidrun () { while(isrunning) {Try { //random hibernation 0-1000 milliseconds for data acquisition (time consuming to produce data)Thread.Sleep (R.nextint (1000)); //get the data to accumulate ... intID =Count.incrementandget (); //For example, a GetData method is used to obtainData data =NewData (ID), "integer.tostring" +ID); System.out.println ("Current thread:" + thread.currentthread (). GetName () + ", gets the data, ID:" + ID + ", load into public buffer ..."); if(! This. Queue.offer (Data, 2, Timeunit.seconds)) {System.out.println ("Failed to commit buffer data ...."); //Do something ... Like resubmit . } } Catch(interruptedexception e) {e.printstacktrace (); } } } Public voidStop () { This. isrunning =false; } }
Packagecom.bjsxt.height.design016;ImportJava.util.Random;ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.TimeUnit; Public classConsumerImplementsrunnable{PrivateBlockingqueue<data>queue; PublicConsumer (blockingqueue queue) { This. Queue =queue; } //Random Objects Private StaticRandom r =NewRandom (); @Override Public voidrun () { while(true){ Try { //Get DataData data = This. Queue.take (); //for data processing. Sleep 0-1000 milliseconds simulation time-consumingThread.Sleep (R.nextint (1000)); System.out.println ("Current consumer thread:" + thread.currentthread (). GetName () + ", consumption is successful, consumption data is ID:" +Data.getid ()); } Catch(interruptedexception e) {e.printstacktrace (); } } }}
Packagecom.bjsxt.height.design016;ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.LinkedBlockingQueue; Public classMain { Public Static voidMain (string[] args)throwsException {//Memory Buffersblockingqueue<data> queue =NewLinkedblockingqueue<data> (10); //producersProvider P1 =NewProvider (queue); Provider P2=NewProvider (queue); Provider P3=NewProvider (queue); //ConsumerConsumer C1 =NewConsumer (queue); Consumer C2=NewConsumer (queue); Consumer C3=NewConsumer (queue); //Create a thread pool run, which is a cached thread pool that can create infinitely large threads that do not create threads when there are no tasks. Idle thread Survival Time is 60s (default value)Executorservice Cachepool=Executors.newcachedthreadpool (); Cachepool.execute (p1); Cachepool.execute (p2); Cachepool.execute (p3); Cachepool.execute (C1); Cachepool.execute (C2); Cachepool.execute (C3); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); } p1.stop (); P2.stop (); P3.stop (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } //Cachepool.shutdown ();//Cachepool.shutdownnow (); } }
The running result of the program is:
Current thread: pool-1-thread-2, get data, ID: 1, load into public buffer ...
Current thread: pool-1-thread-1, get data, ID: 2, load into public buffer ...
Current thread: pool-1-thread-3, get data, ID: 3, load into public buffer ...
Current thread: pool-1-thread-2, get data, ID: 4, load into public buffer ...
Current consumption thread: pool-1-thread-6, consumption is successful, consumption data is Id:3
Current consumption thread: pool-1-thread-4, consumption is successful, consumption data is id:1
Current thread: pool-1-thread-3, get data, ID: 5, load into public buffer ...
Current thread: pool-1-thread-3, get data, ID: 6, load into public buffer ...
Current thread: pool-1-thread-1, get data, ID: 7, load into public buffer ...
Current consumption thread: pool-1-thread-4, consumption is successful, consumption data is Id:5
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is Id:2
Current thread: pool-1-thread-2, get data, ID: 8, load into public buffer ...
Current consumption thread: pool-1-thread-6, consumption is successful, consumption data is Id:4
Current thread: pool-1-thread-3, get data, ID: 9, load into public buffer ...
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is Id:7
Current thread: pool-1-thread-3, get data, ID: 10, load into public buffer ...
Current consumption thread: pool-1-thread-6, consumption is successful, consumption data is Id:8
Current thread: pool-1-thread-1, get data, ID: 11, load into public buffer ...
Current thread: pool-1-thread-1, get data, ID: 12, load into public buffer ...
Current thread: pool-1-thread-2, get data, ID: 13, load into public buffer ...
Current consumption thread: pool-1-thread-4, consumption is successful, consumption data is Id:6
Current thread: pool-1-thread-3, get data, ID: 14, load into public buffer ...
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is Id:9
Current thread: pool-1-thread-2, get data, ID: 15, load into public buffer ...
Current thread: pool-1-thread-1, get data, ID: 16, load into public buffer ...
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is Id:12
Current thread: pool-1-thread-3, get data, ID: 17, load into public buffer ...
Current consumption thread: pool-1-thread-6, consumption is successful, consumption data is id:10
Current thread: pool-1-thread-2, get data, ID: 18, load into public buffer ...
Current consumption thread: pool-1-thread-4, consumption is successful, consumption data is id:11
Current thread: pool-1-thread-1, get data, ID: 19, load into public buffer ...
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is id:13
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is id:16
Current consumption thread: pool-1-thread-4, consumption is successful, consumption data is id:15
Current thread: pool-1-thread-3, get data, ID: 20, load into public buffer ...
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is id:17
Current consumption thread: pool-1-thread-6, consumption is successful, consumption data is id:14
Current consumption thread: pool-1-thread-4, consumption is successful, consumption data is id:18
Current consumption thread: pool-1-thread-5, consumption is successful, consumption data is id:19
Current consumption thread: pool-1-thread-6, consumption is successful, consumption data is id:20
Pretty classic.
Thread Pool Foundation One