A collaboration between the threads, how to achieve?
1. Polling: it is difficult to ensure the timely, large resource overhead
2, waiting and notification
Standard paradigm of waiting and notification:
Wait square:
go to acquire the lock object,
and then judge in the loop condition is satisfied, not satisfied to call the wait method.
Conditions are met, the implementation of business logic
notifying party:
acquiring an object lock
changing conditions
notify all waiting in the thread object
3. Methods:
wait (): waiting for lock acquisition target
wait (1000): Timeout waiting for more than a certain time does not wait.
notify: notify a thread
notifyAll: notify all waiting for the same lock thread
Two, join () method
1, interview questions: have thread A and thread B, how to ensure that thread B must only be performed after executing the thread A?
Method One: join ()
Method Two: countDownLatch
Explanation: If thread A method to perform a join thread B, thread after thread B A must wait for execution is over, the thread A can continue their work.
Sample Code: off with the join () method, allowing the thread execution priority
/ * * * Demo join () method * / public class UseJoin2 { static class JumpQueue the extends the Thread { @Override public void RUN () { for ( int I = 0 ; I < . 5 ; I ++ ) { . The System OUT .println ( " the current thread that is performing step " + I); the try { the Thread.sleep ( 1000 ); } the catch (InterruptedException E) { e.printStackTrace (); } } } } public static void main (String [] args) { the System. OUT .println ( " into the main method ------> " ); JumpQueue jumpQueue = new new JumpQueue (); jumpQueue .setName ( " joinThread " ); jumpQueue.start (); // jumpQueue.join () method: make jumpQueue thread executing the main thread before the try { jumpQueue.join (); } the catch (InterruptedException E) { e.printStackTrace (); } the System. OUT .println ( " main thread start processing business logic " ); the try { the Thread.sleep ( 500 ); } the catch (InterruptedException E) { e.printStackTrace (); } . the System OUT . the println ( " main thread has finished the processing of business logic " ); } }
Three, yield (), sleep () , wait (), notify () and other methods of influence on the lock
thread after the execution yield (), holds the lock is not released
after the sleep () method call, hold the lock is not released
wait (): before calling wait () method, it is necessary to hold the lock. After the call to wait () method. The lock will be released (release the virtual machine), when the wait method returns, the thread holding the lock will re-
notify (): Before calling, we must hold the lock. Call the notify () method itself will not release the lock, only the synchronized code block has been executed before releasing the lock play
notifyAll (): with notify ()
, such as: public void changeKm synchronized () {
this.km = 101;
notify (); / / when executes this line of code, the lock is not released at this time.
System.out.println ( "business logic"); // End after the implementation of this line, and releases the lock.
}
1, the sample code: sleep () method call after holding the lock is not released identification
/ * * * Verification sleep () method, the lock does not release the thread is sleeping * When A thread to get the lock, then sleep sleep. Other threads can not get the lock, can only wait * / public class SleepLock { Private Object Lock = new new Object (); // sleeps thread class Private class ThreadSleep the extends the Thread { @Override public void RUN () { String threadName = Thread.currentThread () getName ();. // try to get a lock System. OUT .println (threadName + " the try to the Take at The lock " ); the try{ The synchronized ( Lock ) { the System. OUT .println (threadName + " IS Taking The Lock " ); // after the thread to get the lock, sleep five seconds the Thread.sleep ( 5000 ); . The System OUT .println ( " Finish The Work: " + threadName); } } the catch (InterruptedException E) { } } } // do not dormant thread class Private class ThreadNotSleep extends Thread{ @Override public void run() { String threadName = Thread.currentThread().getName(); //尝试去拿锁 System.out.println(threadName + " try to take the lock time = "+System.currentTimeMillis()); synchronized (lock){ System.out.println(threadName + " taking the lock time = "+ System.currentTimeMillis()); System.out.println("Finish the work"+threadName); } } } public static void main(String[] args) { SleepLock sleepLock = new SleepLock(); //会休眠的线程 Thread threadSleep = sleepLock.new ThreadSleep(); threadSleep.setName("threadSleep"); //不会休眠的线程 Thread threadNotSleep = sleepLock.new ThreadNotSleep(); threadNotSleep.setName("threadNotSleep"); threadSleep.start(); threadNotSleep.start(); } }
2, the sample code: after wait () method call, the lock will release
public class ThreadDomain31 extends Thread{ private Object lock; public ThreadDomain31(Object object){ this.lock = object; } @Override public void run() { try { synchronized (lock) { System.out.println(Thread.currentThread().getName() + " Begin wait()"); lock.wait(); System.out.println(Thread.currentThread().getName() + " End wait"); } } catch (InterruptedException e) { e.printStackTrace(); } } }
public class the Test { public static void main (String [] args) { / * * * output: * the Thread-0 the Begin the wait () the Thread-the wait. 1 the Begin () the wait () releases the lock, or simply the entrance thread 2 * / Object Lock = new new Object (); ThreadDomain31 mt0 on the worksheet = new new ThreadDomain31 ( Lock ); ThreadDomain31 MTl = new new ThreadDomain31 ( Lock ); mt0.start (); mt1.start (); } }
3, classic producer consumer model demonstrates wait / notify