Multi-threaded thread communication Condition condition and multi-threaded Condition
Condition is the implementation of wake-up and blocking between communication threads under the Locks lock. The following await and signal can implement all the functions of wait, policy, and policyall in the Object. In addition, the monitor can be changed and bound to each condition to implement multi-condition listening. Condition is actually bound to a Lock. The Lock instance with a waist circumference obtains the Condition, that is, the newCondition () method.
The await () in Condition is relative to the wait () in the Object. It is in the waiting state before it is blocked or interrupted.
Condition y () is equivalent to Signal () under the Object; wake up a waiting state.
Condition application:
Suppose there is a cache area with a size of 100. It supports the put and take methods. If you try to perform the take operation in an empty cache area, the thread is blocked until a certain item becomes available. If you try to perform the operation in a full cache area, before the space becomes available, the thread is always congested. We can use two conditions: full record cache and empty record cache.
Package andy. thread. test; import java. util. concurrent. locks. condition; import java. util. concurrent. locks. lock; import java. util. concurrent. locks. reentrantLock;/*** @ author Zhang, Tianyou * @ version November 9, 2014 11:39:30 */public class ThreadConditiionBoundedBuffer {public static void main (String [] args) {BoundedBuffer buffer = new ThreadConditiionBoundedBuffer (). new BoundedBuffer (); for (int I = 0; I <100; I ++) {int task = I; new Thread (new Runnable () {public void run () {try {String putString = Thread. currentThread (). getName () + task; buffer. put (putString); System. out. println (putString);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}})). start () ;}for (int I = 0; I <100; I ++) {new Thread (new Runnable () {public void run () {try {System. out. println (Thread. currentThread () + "take" + buffer. take ();} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}})). start () ;}} class BoundedBuffer {// defines Lock lock = new ReentrantLock (); Condition notFull = Lock. newCondition (); // No full Condition notEmpty = lock. newCondition (); // not empty Object [] items = new Object [100]; int putptr, takeptr, count; public void put (Object x) throws InterruptedException {lock. lock (); try {// wait for while (count = items. length) notFull. await (); // if the execution is not full, add items [putptr] = x; if (++ putptr = items. length) putptr = 0; ++ count; // wake up the notEmpty read buffer thread. signal ();} finally {lock. unlock () ;}} public Object take () throws InterruptedException {lock. lock (); try {// wait for while (count = 0) notEmpty if it is empty. await (); // not empty go to Object x = items [takeptr] in order; if (++ takeptr = items. length) takeptr = 0; -- count; notFull. signal (); return x;} finally {lock. unlock ();}}}}
The execution result is as follows:
Thread-00Thread-22Thread-11Thread-1414Thread-1212Thread-1010Thread-88Thread-66Thread-44Thread-1616Thread-1818Thread-77Thread-2222Thread-2020Thread-55Thread-33Thread-2424Thread-2626Thread-99Thread-2828Thread-1313Thread-3030Thread-1717Thread-1111Thread-3232Thread-1919Thread-1515Thread-2121Thread-2323Thread-2525Thread-3434Thread-3131Thread-2727Thread-3333Thread-2929Thread-3535Thread-3636Thread-3737Thread-3838Thread-4040Thread-3939Thread-4242Thread-6464Thread-6666Thread-6262Thread-6060Thread-5858Thread-4747Thread-5656Thread-4545Thread-5050Thread-4848Thread-5252Thread-4646Thread-5454Thread-4444Thread-4343Thread-6868Thread-4141Thread-7070Thread-7272Thread-7474Thread-7676Thread-4949Thread-7878Thread-6969Thread-8080Thread-7171Thread-8282Thread-7373Thread-7575Thread-8484Thread-8686Thread-7777Thread-8888Thread-8181Thread-9090Thread-8989Thread-8383Thread-7979Thread-8787Thread-8585Thread-6767Thread-9292Thread-9191Thread-6565Thread-9494Thread-6363Thread-6161Thread-9696Thread-9898Thread-5959Thread-5757Thread-5555Thread-5353Thread-9393Thread-9595Thread[Thread-100,5,main]take Thread-00Thread-9999Thread[Thread-101,5,main]take Thread-22Thread[Thread-102,5,main]take Thread-11Thread-9797Thread[Thread-104,5,main]take Thread-44Thread[Thread-106,5,main]take Thread-66Thread[Thread-108,5,main]take Thread-88Thread[Thread-110,5,main]take Thread-1010Thread-5151Thread[Thread-112,5,main]take Thread-1212Thread[Thread-114,5,main]take Thread-1616Thread[Thread-116,5,main]take Thread-1818Thread[Thread-118,5,main]take Thread-77Thread[Thread-120,5,main]take Thread-2020Thread[Thread-122,5,main]take Thread-2222Thread[Thread-124,5,main]take Thread-55Thread[Thread-103,5,main]take Thread-1414Thread[Thread-126,5,main]take Thread-33Thread[Thread-128,5,main]take Thread-2424Thread[Thread-105,5,main]take Thread-2626Thread[Thread-130,5,main]take Thread-99Thread[Thread-107,5,main]take Thread-2828Thread[Thread-132,5,main]take Thread-1313Thread[Thread-109,5,main]take Thread-3030Thread[Thread-134,5,main]take Thread-1515Thread[Thread-111,5,main]take Thread-1111Thread[Thread-136,5,main]take Thread-1717Thread[Thread-113,5,main]take Thread-3232Thread[Thread-138,5,main]take Thread-1919Thread[Thread-140,5,main]take Thread-2121Thread[Thread-115,5,main]take Thread-2323Thread[Thread-142,5,main]take Thread-2525Thread[Thread-117,5,main]take Thread-3434Thread[Thread-144,5,main]take Thread-3131Thread[Thread-119,5,main]take Thread-2727Thread[Thread-146,5,main]take Thread-2929Thread[Thread-148,5,main]take Thread-3333Thread[Thread-121,5,main]take Thread-3535Thread[Thread-150,5,main]take Thread-3636Thread[Thread-152,5,main]take Thread-3737Thread[Thread-123,5,main]take Thread-4040Thread[Thread-125,5,main]take Thread-3838Thread[Thread-127,5,main]take Thread-3939Thread[Thread-154,5,main]take Thread-4242Thread[Thread-129,5,main]take Thread-6666Thread[Thread-156,5,main]take Thread-6464Thread[Thread-131,5,main]take Thread-6262Thread[Thread-158,5,main]take Thread-6060Thread[Thread-160,5,main]take Thread-5656Thread[Thread-137,5,main]take Thread-4545Thread[Thread-133,5,main]take Thread-5858Thread[Thread-135,5,main]take Thread-4747Thread[Thread-162,5,main]take Thread-5454Thread[Thread-139,5,main]take Thread-5252Thread[Thread-164,5,main]take Thread-5050Thread[Thread-143,5,main]take Thread-4646Thread[Thread-141,5,main]take Thread-4848Thread[Thread-145,5,main]take Thread-4444Thread[Thread-166,5,main]take Thread-4343Thread[Thread-147,5,main]take Thread-6868Thread[Thread-168,5,main]take Thread-4141Thread[Thread-157,5,main]take Thread-7070Thread[Thread-151,5,main]take Thread-7272Thread[Thread-170,5,main]take Thread-7474Thread[Thread-155,5,main]take Thread-7676Thread[Thread-153,5,main]take Thread-4949Thread[Thread-172,5,main]take Thread-7878Thread[Thread-149,5,main]take Thread-6969Thread[Thread-174,5,main]take Thread-8080Thread[Thread-159,5,main]take Thread-7171Thread[Thread-176,5,main]take Thread-8282Thread[Thread-161,5,main]take Thread-7373Thread[Thread-163,5,main]take Thread-7575Thread[Thread-178,5,main]take Thread-8484Thread[Thread-165,5,main]take Thread-8686Thread[Thread-167,5,main]take Thread-7777Thread[Thread-180,5,main]take Thread-8888Thread[Thread-182,5,main]take Thread-8181Thread[Thread-169,5,main]take Thread-9090Thread[Thread-184,5,main]take Thread-8989Thread[Thread-171,5,main]take Thread-8383Thread[Thread-173,5,main]take Thread-7979Thread[Thread-189,5,main]take Thread-8585Thread[Thread-183,5,main]take Thread-6161Thread[Thread-192,5,main]take Thread-5959Thread[Thread-187,5,main]take Thread-9898Thread[Thread-185,5,main]take Thread-9696Thread[Thread-179,5,main]take Thread-6363Thread[Thread-194,5,main]take Thread-5757Thread[Thread-181,5,main]take Thread-9494Thread[Thread-190,5,main]take Thread-6565Thread[Thread-177,5,main]take Thread-9191Thread[Thread-196,5,main]take Thread-5555Thread[Thread-175,5,main]take Thread-9292Thread[Thread-188,5,main]take Thread-6767Thread[Thread-191,5,main]take Thread-5353Thread[Thread-198,5,main]take Thread-9393Thread[Thread-186,5,main]take Thread-8787Thread[Thread-193,5,main]take Thread-9595Thread[Thread-195,5,main]take Thread-9999Thread[Thread-197,5,main]take Thread-9797Thread[Thread-199,5,main]take Thread-5151