Use of Java concurrency Programming _wait and notify (iii)

Source: Internet
Author: User
Tags thread stop

Package Sync;import Java.util.arraylist;import Java.util.list;public class Waitandnotify {private volatile static List list= new ArrayList ();p rivate void Add () {list.add ("Wang"); private int size () {return list.size ();} public static void Main (string[] args) {final waitandnotify list1 = new Waitandnotify ();/* * 1. Instantiate an object as a lock, below T1 and T2 competition This lock is implemented in sync * 2. There is a wait, notify method under object (interview often asks: What are the methods under Object) * 3. Note that each object has both methods and can be treated as a lock */final object lock = new Object (); Thread t1 = new Thread (new Runnable () {@Overridepublic void run () {try {synchronized (lock) {for (int i = 0; i <; i++ ) {List1.add (); System.out.println ("Current thread:" + thread.currentthread (). GetName () + "added an element"); Thread.Sleep (+), if (list1.size () = = 5) {System.out.println ("notification has been issued:"); /notify does not release the lock, the for loop will continue to execute lock.notify ();}}} catch (Interruptedexception e) {e.printstacktrace ();}}}, "T1"); Thread t2 = new Thread (new Runnable () {@Overridepublic void run () {synchronized (lock) {/* * * If the T2 thread is executed first, if size is not 5, wait * WA It releases the lock */if (list1.size ()! = 5) {try {System.out.println ("T2 Enter:"); Thread.Sleep (+); lock.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}} System.out.println ("Current thread received notification:" + thread.currentthread (). GetName () + "List size = 5, thread stop"); throw new RuntimeException ( }}}, "T2"); T2.start (); T1.start ();}}

Output Result:

T2 Enter: Current thread: T1 adds an element to the current thread: T1 adds an element to the current thread: T1 adds an element to the current thread: T1 adds an element to the current thread: T1 added an element has been notified. Current thread: T1 adds an element to the current thread: T1 adds an element to the current thread: T1 adds an element to the current thread: T1 adds an element to the current thread: T1 added an element the current thread receives notification: t2list size = 5, thread stop exception in Thread "T2" Java.lang.RuntimeExceptionat sync. Waitandnotify$2.run (waitandnotify.java:73) at Java.lang.Thread.run (Unknown Source)

  

Code interpretation:

The first step: Execute T2 thread, enter the Run method, list.size not equal to 5, lock.wait release lock, T2 process Wait, turn to execute T1

Second step: Execute T1 process, get lock, execute for Loop, notify when list.size equals 5 o'clock. Wakes the T2 process, but continues execution of the For loop because the notify does not release the lock

Step three: T2 process is awakened, so list.size already equals 10, not equal to 5, directly outputs the last two lines of code

Use of Java concurrency Programming _wait and notify (iii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.