First write, the wrong place, I hope you point out, thank you!
Wait, notify are all methods in object:
1, they must match synchronized keyword use
2,wait method Release Lock, notify method does not release lock
Requirements: A collection, 2 threads, one thread adds 10 elements to the collection, and another thread determines that a piece of code is executed if the collection is exactly 5 elements;
1 Public classLISTADD2 {23 Private StaticList List =NewArrayList ();4 Public voidAdd () {5List.add ("Hello World");6 }7 8 Public intsize () {9 returnlist.size ();Ten } One A - Public Static voidMain (string[] args) { - theLISTADD2 Listadd =NewListAdd2 (); - - //here, define a lock . - FinalObject lock =NewObject (); + -Thread T1 =NewThread (NewRunnable () { + @Override A Public voidrun () { at synchronized(lock) { -System.out.println ("T1 start"); - for(inti = 0; I < 10; i++) { - Try { - Listadd.add (); -System.out.println ("Current thread:" in+Thread.CurrentThread (). GetName () -+ "added an element ..."); toThread.Sleep (500); + if(listadd.size () = = 5) { -SYSTEM.OUT.PRINTLN ("T1 notification"); theLock.notify ();//Lock notification does not release the lock * } $ }Panax Notoginseng Catch(interruptedexception e) { - e.printstacktrace (); the } + } A } the } + }); - $Thread t2 =NewThread (NewRunnable () { $ - @Override - Public voidrun () { the - synchronized(lock) {WuyiSystem.out.println ("T2 start"); the if(Listadd.size ()! = 5) { - Try { WuLock.wait ();//not equal to 5 thread 2 just wait here -}Catch(interruptedexception e) { About e.printstacktrace (); $ } - } -System.out.println ("Current thread:" -+ Thread.CurrentThread (). GetName () + "receive notification thread stop:"); A Throw Newruntimeexception (); + } the } - }); $ T2.start (); the T1.start (); the } the}
The printing results are as follows:
T2 start
T1 start
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
T1 to give notice
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: THREAD-1 received notification thread stopped.
Exception in Thread "Thread-1" java.lang.RuntimeException
At Com.bjsxt.base.conn008.listadd2$2.run (listadd2.java:66)
At Java.lang.Thread.run (thread.java:748)
First thread 2 starts execution first, acquires lock lock, waits in the Wait () method, and releases the lock.
Thread 1 acquires the lock lock, adds 5 elements, sends a notification, wakes the lock, but at this point the thread 1 does not release the lock, but continues execution, and when the loop ends, the lock is released
Thread 2 continues to run, executing the subsequent business logic.
This does not meet our business needs, we want the collection of elements once there are 5, the execution of thread 2, rather than wait until the completion of all threads 1, the execution of thread 2;
So here we're going to use the class in the Java.util.concurrent package Countdownlatch
Public Static voidMain (string[] args) {ListAdd2 listadd=NewListAdd2 (); //here, define a lock .//Final Object lock = new Object ();Countdownlatch Countdownlatch =NewCountdownlatch (1);//here the 1 represents the counter for 1Thread T1 =NewThread (NewRunnable () {@Override Public voidrun () {//synchronized (lock) {System.out.println ("T1 start"); for(inti = 0; I < 10; i++) { Try{listadd.add (); System.out.println ("Current thread:" +Thread.CurrentThread (). GetName ()+ "added an element ..."); Thread.Sleep (500); if(listadd.size () = = 5) {System.out.println ("T1 to give notice"); Countdownlatch.countdown (); //Decrements the Count of latches, and if the count reaches 0, releases all waiting threads. //lock.notify ();//Lock notification does not release the lock } } Catch(interruptedexception e) {e.printstacktrace (); } } }// } }); //Start thread 1Thread t2 =NewThread (NewRunnable () {@Override Public voidrun () {//synchronized (lock) {System.out.println ("T2 start"); if(Listadd.size ()! = 5) { Try{countdownlatch.await (); //thread waits //lock.wait ();//not equal to 5 thread 2 just wait here}Catch(Exception e) {e.printstacktrace (); }} System.out.println ("Current thread:" + thread.currentthread (). GetName () + "receive notification thread stop ..."); Throw Newruntimeexception (); }// } }); T2.start (); T1.start (); }
Print Result: Executes Countdownlatch.countdown () when the element in the collection is 5 o'clock; Releasing all waiting threads, thread 2 executes immediately.
If Countdownlatch countdownlatch = new Countdownlatch (2); Set to 2 to execute 2 times, Countdownlatch.countdown (), which will release all waiting threads when the counter is 0 o'clock
T2 start
T1 start
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
T1 to give notice
Current thread: Thread-0 an element has been added ...
Current thread: THREAD-1 received notification thread stopped.
Exception in Thread "Thread-1" java.lang.RuntimeException
At Com.bjsxt.base.conn008.listadd2$2.run (listadd2.java:69)
At Java.lang.Thread.run (thread.java:748)
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Current thread: Thread-0 an element has been added ...
Some simple applications of Java multithreading Wait,notify,countdownlatch