Previously written: synchronize to implement producer and consumer problems
Now we use Lock to implement it.
Basket B = Product p = Consumer c = Consumer c1 = ManTou (. id = "ManTou" + max = 6 character list <ManTou> manTous = Character List <ManTou> Lock lock = ReentrantLock (); Condition full = lock. newCondition (); Condition empty = lock. newCondition (); (max = System. out. println ("the basket is full and will be reproduced later... "} ManTou m = (manTous. size () = 0 System. out. println ("the basket is empty. Please try again later... "m =} Product. basket = (I = 0; I <40; I ++ ManTou m = System. out. println ("produced" + Thread. sleep () (Math. random () * 2000} Consumer. basket = (I = 0; I <20; I ++ Thread. sleep () (Math. random () * 2000} ManTou m = System. out. println ("consumed" +}
Appendix: differences between synchronize and Lock