Not much to say, directly to the last example, the famous producer consumer problem.
1 Public classProducerconsumer {2 Public Static voidMain (string[] args) {3Syncstack SS =NewSyncstack ();4Producer p =NewProducer (ss);5Consumer C =NewConsumer (ss);6 NewThread (P). Start ();//Producer Threads7 NewThread (c). Start ();//Consumer Threads8 }9 }Ten /** One items Consumed---The TOU A **/ - classWotou { - intID; theWotou (intID) { - This. ID =ID; - } - PublicString toString () { + return"Wotou:" +ID; - } + } A /** at consumption of space---stacks - **/ - classSyncstack { - intindex = 0; -wotou[] ARRWT =NewWotou[6]; - in Public synchronized voidpush (Wotou wt) { - while(index = = arrwt.length) {//it is the while and not the if, and when wait is interrupted, you still have to check if the stack is full. to Try { + This. Wait (); -}Catch(interruptedexception e) { the e.printstacktrace (); * } $ }Panax Notoginseng This. Notifyall (); -Arrwt[index] =wt; theIndex + +; + } A the Public synchronizedWotou Pop () { + while(Index = = 0) { - Try { $ This. Wait (); $}Catch(interruptedexception e) { - e.printstacktrace (); - } the } - This. Notifyall ();Wuyiindex--; the returnArrwt[index]; - } Wu } - About /** $ Producer Class - **/ - classProducerImplementsRunnable { -Syncstack SS =NULL; A Producer (Syncstack ss) { + This. SS =SS; the } - $ Public voidrun () { the for(inti=0; i<20; i++) { theWotou wt =NewWotou (i); the ss.push (WT); theSystem.out.println ("produced:" +wt); - Try { inThread.Sleep ((int) (Math.random () * 200)); the}Catch(interruptedexception e) { the e.printstacktrace (); About } the } the } the } + /** - Consumer class the **/Bayi classConsumerImplementsRunnable { theSyncstack SS =NULL; the Consumer (Syncstack ss) { - This. SS =SS; - } the the Public voidrun () { the for(inti=0; i<20; i++) { theWotou wt =Ss.pop (); -System.out.println ("Consumed:" +wt); the Try { theThread.Sleep ((int) (Math.random () * 1000)); the}Catch(interruptedexception e) {94 e.printstacktrace (); the } the } the }98}
Effect
The introduction of synchronized, learning database system and operating system of friends should have some understanding, for shared resources, when a thread occupies the time can not be manipulated by other threads, and operating system atomic operation principle is somewhat similar. When synchronized, an object lock is acquired, and other threads cannot acquire an object lock.
Wait Introduction: For example, the upper stack can only put 6 of the head, but when the stack has 6 of the head, the producer can not produce the object lock, so the producer will enter the wait pool, but also release the object lock.
Introduction of Notify: Wake up a thread in the wait pool (PS: Can only wake up other threads, cannot wake itself)
Introduction of Notifyall: Waking all threads in the wait pool
In the end, let's say the difference between wait and sleep, personal understanding:
1.wait is a method in the object class that throws Interuptexception,sleep is a method in the thread class, and the wait method is used only when the object is obtained, otherwise it throws an exception;
The 2.wait method frees the object lock, but sleep does not release the object lock;
A thread after 3.wait requires other threads to wake up, but the sleep thread automatically wakes up after a specified time.
Java Thread synchronization problem