In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what is the difference between sleep and wait methods in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between sleep and wait in Java?"
I. the difference between sleep and wait methods
Fundamental difference: sleep is a method in the Thread class and does not run immediately. Wait is a method in the Object class. Once an object calls the wait method, you must use the notify () and notifyAll () methods to wake up the process.
Release synchronization locks: sleep releases cpu, but sleep does not release synchronization lock resources, and wait releases synchronization lock resources
Scope of use: sleep can be used anywhere, but wait can only be used in synchronized synchronization methods or code blocks
Exception handling: sleep needs to catch exceptions, while wait does not need to catch exceptions
2. Wait method
Causes the currently executing thread to wait. (put the thread in the waiting queue)
Release the current lock
Wake up when certain conditions are met and try to acquire the lock again.
Wait should be used with synchronized. Using wait without synchronized will directly throw an exception.
Use of the wait method
Wait method
/ * use of wait * / public class WaitDemo1 {public static void main (String [] args) {Object lock = new Object (); Thread T1 = new Thread (()-> {System.out.println ("Thread 1 starts execution") Try {synchronized (lock) {System.out.println ("Thread 1 calls the wait method...."); / / indefinite wait status lock.wait ();} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("thread 1 execution completed");}, "thread 1"); t1.start ();}}
Wait threads with parameters and wait threads without parameters
/ * * Parametric wait threads and nonparametric wait threads * / public class WaitDemo2 {public static void main (String [] args) {Object lock1 = new Object (); Object lock2 = new Object (); Thread T1 = new Thread (()-> {System.out.println ("Thread 1 starts execution") Synchronized (lock1) {try {lock1.wait ();} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("Thread 1 execution completed");}}, "No parameter wait thread") T1.start (); Thread T2 = new Thread (()-> {System.out.println ("Thread 2 starts execution"); synchronized (lock2) {try {lock2.wait (60 / 60 / 1000);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("Thread 2 execution completed");}}, "Parametric wait Thread"); t2.start ();}} conditions for wait to end waiting
① other threads call the object's notify method.
The ② wait wait time timed out (the wait method provides a version with the timeout parameter to specify the wait time).
③ other threads call the interrupted method of the waiting thread, causing wait to throw an InterruptedException exception
3. Notify and notifyAll methods
The notify method simply wakes up a waiting thread
The method notify () is also called in the synchronization method or synchronization block, which is used to notify other threads that may be waiting for the object lock of the object.
If there are multiple threads waiting, randomly pick a thread in wait state
After the notify () method, the current thread will not release the object lock immediately, but will not release the object lock until the thread executing the notify () method finishes executing the program, that is, after exiting the synchronous code block.
Use of the notify method
/ * the use of wait, if there are multiple threads waiting, randomly pick a thread with wait status * / public class WaitNotifyDemo {public static void main (String [] args) {Object lock1 = new Object (); Object lock2 = new Object (); Thread T1 = new Thread (()-> {System.out.println ("Thread 1 starts execution") Try {synchronized (lock1) {System.out.println ("Thread 1 calls wait method"); lock1.wait ();}} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("Thread 1 execution completed");}, "Thread 1"); Thread T2 = new Thread (()-> {System.out.println ("Thread 2 starts execution"); try {synchronized (lock1) {System.out.println ("Thread 2 calls the wait method") Lock1.wait ();}} catch (InterruptedException e) {e.printStackTrace ();} System.out.println (Thread 2 execution completed);}, Thread 2); t1.start (); t2.start () / / Wake up (randomly awaken one) Thread T3 = new Thread (()-> {try {Thread.sleep (1500);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("Thread 3 starts execution") of the dormant thread on the lock1 object Synchronized (lock1) {/ / Wake up notification System.out.println ("Wake up performed"); try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace () }, "Thread 3"); t3.start ();}}
The notifyAll method can wake up all waiting threads at once
Use of the notifyAll method
/ * notifyAll- wakes up all threads * / public class WaitNotifyAll {public static void main (String [] args) {Object lock = new Object (); new Thread (()-> {System.out.println ("Thread 1: start execution"); synchronized (lock) {try {lock.wait () } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("Thread 1: execution completed");}}, "No parameter wait thread" .start () New Thread (()-> {synchronized (lock) {System.out.println ("Thread 2: start execution |" + LocalDateTime.now ()); try {lock.wait (60 * 60 * 60 * 1000);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("Thread 2: execution completed |" + LocalDateTime.now ());}}, "Parametric wait Thread". Start (); new Thread (()-> {try {TimeUnit.SECONDS.sleep (1)) } catch (InterruptedException e) {e.printStackTrace ();} synchronized (lock) {System.out.println ("Wake up all threads"); lock.notifyAll ();}}) .start ();}}
The difference between notify and notifyAll methods
When you call notify, only one waiting thread will be awakened and there is no guarantee which thread will be awakened, depending on the thread scheduler.
When the notifyAll method is called, all threads waiting for the lock will be awakened, but all awakened threads will compete for the lock before the rest of the code is executed, which is why wait is called on the loop, because if multiple threads are awakened, the thread will get the lock and will execute first, and it may reset the wait condition, which will force subsequent threads to wait.
Therefore, the key difference between notify and notifyAll is that notify () wakes up only one thread, while the notifyAll method wakes up all threads.
At this point, I believe you have a deeper understanding of "what is the difference between sleep and wait methods in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.