Reprint Please specify source: http://blog.csdn.net/sunyujia/
Forum on the question raised by the Netizen, each production 3 can only consume 2, feel not very difficult to do so, in order to save trouble
Modified on the basis of the original http://blog.csdn.net/sunyujia/archive/2008/05/02/2362015.aspx.
The only difficulty is that the production of 3 threads is not the same thread. A thread that consumes 2 threads is not the same thread. That is, a thread can not be a continuous production of 3, a thread of continuous consumption of 2, if a thread continuous production, the program is not interesting. import java.util.arraylist; import java.util.list; import java.util.concurrent.executorservice; import java.util.concurrent.executors; /** * interesting producer consumer issues * 3 per generation of 2 * @author: sunyujia * @blog: http://blog.csdn.net/sunyujia/ * @mail: sunyujia@sunyujia@yahoo.cn */Public class executorservicetest { private static int producecount = 0; private static int consumeCount = 0; private static object producelock = new object (); private static object consumelock = new object (); public static void main (String[] args) { &nbsP; final list tasks = new arraylist (); system.out.println ("myblog: http://blog.csdn.net/" + " sunyujia/"); final ExecutorService exec = Executors.newcachedthreadpool (); for (int i = 0; i < 10; i++) {//10 a producer exec.execute ( New runnable () { public void run () { while (! Thread.interrupted ()) { try { synchronized (Consumelock) { if ( PRODUCECOUNT&NBSP;>=&NBSP;3) consumelock.wait (); } synchronized (producelock) { if (producecount < 3) { tasks.add (New object ());// production system.out.println (Thread.CurrentThread (). GetName () + "Production tasks remaining:" + Tasks.size ()); producecount++; if (producecount >= 3) { consumeCount = 2;// Tips for spending only 2 system.out.println ("producer friendly tips now in stock" + tasks.size ()); producelock.notifyall (); } Thread.yield (); } } thread.sleep (;// ) Let the program perform slowly for observation } catch (interruptedexception e) { e.printstacktrace (); } } } }); } for (int i = 0; i < 20; i++) &NBSP;{//20 Consumer exec.execute (new runnable () { public void run () { while (! Thread.interrupted ()) { try { synchronized (producelock) { if (consumecount <= 0) Producelock.wait (); } synchronized (consumelock) { if (consumecount > 0) { tasks.remove (0);// consumption system.out.println ( Thread.CurrentThread (). GetName () + "Processing tasks remaining:" + tasks.size ()); consumecount--; if (consumecount <= 0) { produceCount = 0;// Requires 3 more &nbs to be generatedp; &NBSP;SYSTEM.OUT.PRINTLN ("Consumer friendly tips now in stock" + tasks.size ()); consumelock.notifyall (); } } } thread.sleep (;// ) Let the program perform slowly for observation } catch (interruptedexception e) { e.printstacktRace (); } } } } ); } exec.shutdown (); }}
The
Execution results are as follows:
Pool-1-thread-1 production tasks left: 1
pool-1-thread-2 production tasks left: 2
Pool-1-thread-4 production tasks left: 3
Manufacturer friendly tips now in stock 3
pool-1-thread-11 Processing tasks remaining: 2
pool-1-thread-12 processing tasks left: 1
Consumer friendly tips now in stock 1
pool-1-thread-5 production tasks remaining: 2
Pool-1-thread-6 production Tasks Left: 3
Pool-1-thread-7 production tasks left: 4
Producer friendly Tips now in stock 4
pool-1-thread-14 processing tasks remaining: 3
Pool-1-thread-16 processing tasks Left: 2
Consumer friendly tips now in Stock 2
pool-1-thread-3 production tasks remaining: 3
Pool-1-thread-2 production tasks left: 4
Pool-1-thread-4 production Tasks Left: 5
Producer friendly Tips now in stock 5