Synchronous running means that multiple threads that need to transmit data must be synchronized and consistent in steps to ensure that the transmitted data is received in a timely and accurate manner. Obviously, only the keyword synchronized is not enough, although the keyword synchronized prevents concurrent updates to an object, it does not implement data transmission between threads. The thread class provides the wait (), Policy (), and policyall () functions inherited from the object class.
Application Example: If the buffer size is 1 while data is put to buffered
Package winter; Class buffer2 {private int value; private Boolean isempty = true; // whether the semaphore synchronized void put (int I) with null value // put data {While (! Isempty) {try {This. wait ();} catch (interruptedexception e) {system. out. println (E. getmessage () ;}} value = I; isempty = false; Policy () ;}synchronized int get () {// get data while (isempty) {try {This. wait ();} catch (interruptedexception e) {system. out. println (E. getmessage () ;}} isempty = true; y (); Return Value ;}} class get2 extends thread {// data fetch thread private buffer2 BF; Public get2 (buffer2 BF) {This. BF = BF;} public void run () {for (INT I = 1; I <6; ++ I) system. out. println ("\ t get2 get:" + BF. get () ;}} public class put2 extends thread {// put data thread/*** @ Param ARGs */private buffer2 BF; Public put2 (buffer2 BF) {This. BF = BF;} public void run () {for (INT I = 1; I <6; ++ I) {BF. put (I); system. out. println ("put2 put:" + I) ;}} public static void main (string [] ARGs) {// todo auto-generated method stub buffer2 BF = new buffer2 (); (new put2 (BF )). start (); (new get2 (BF )). start ();}}
The wait () method changes the current thread to a blocking state and actively releases the mutex lock. This is different from the sleep method. The sleep method does not release the mutex lock.
Y () wakes up other threads waiting for the queue to continue execution. These threads become runable.
Notifyall () wakes up and waits for all threads in the queue to continue execution.
Examples of producers and consumers:
Package winter; Class godown {public static final int max_size = 100; Public int curnum; godown () {} godown (INT num) {This. curnum = num;} public synchronized void produce (INT neednum) {While (this. curnum + neednum> godown. max_size) {system. out. println ("number of products to be produced:" + neednum + "and remaining inventory:" + this. the sum of curnum + "exceeds the maximum inventory, and the production task cannot be executed now !! "); Try {Wait ();} catch (interruptedexception e) {system. out. println (E. getmessage () ;}} this. curnum + = neednum; system. out. println ("already produced" + neednum + "products, current inventory:" + curnum); policyall () ;}public synchronized void consume (INT neednum) {While (neednum> This. curnum) {system. out. println ("to consume" + neednum + "products, greater than the current inventory" + this. curnum + ", cannot consume"); try {Wait ();} catch (interruptedexception e) {system. out. println (E. getmessage () ;}} this. curnum-= neednum; system. out. println ("consumed" + neednum + "products, inventory:" + this. curnum + "Products"); yyall () ;}} class produce extends thread {// producer thread private int neednum; private godown; produce (godown A1, int num) {This. godown = A1; this. neednum = num;} public void run () {This. godown. produce (neednum) ;}} class Consumer extends thread {// consumer thread private int neednum; private godown; Consumer () {} consumer (INT num, godown A1) {This. neednum = num; this. godown = A1;} public void run () {This. godown. consume (neednum) ;}} public class PSC {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubgodown godown = new godown (10); new produce (godown, 10 ). start (); new consumer (20, godown ). start ();}}