learning just encounter these two methods, look up relevant information, and through the program to achieve, the difference:
Each object has a lock to control access to the synchronization, and the Synchronized keyword can interact with an object’s lock to implement Synchronized methods or blocks. sleep() sleep() lock lock! span>). span> wait () method strong> span> is refers to the current thread to temporarily retreat out the synchronous resource lock, so that other threads are waiting for the resource to get the resources, in turn, to run, only calls the notify () method, before calling wait () thread will lift its state of wait, can go to participate in the competition synchronous resource locks, and enforced. ( note: notify role is equivalent to wake the sleeping man, and would not give his assigned task, that is to say notify calling wait before just let the thread has the right to participate in the thread scheduling span>).
2, sleep() can be used anywhere; wait() method can only be used in synchronized methods or synchronized blocks;
3, sleep() is the method of Thread. The call will suspend the specified time of this Thread, but the monitoring will still be maintained, and the object lock will not be released, and it will automatically recover at the time. wait() is an Object method. The call will give up the Object lock and enter the wait queue. The call notify()/notifyAll() will wake up the specified thread or all threads to enter the lock pool and run until the Object lock is acquired again.
to procedures:
public class MultiThread {
private static class Thread1 implements Runnable{
@Override
public void run() {
//由于 Thread1和下面Thread2内部run方法要用同一对象作为监视器,如果用this则Thread1和Threa2的this不是同一对象
//所以用MultiThread.class这个字节码对象,当前虚拟机里引用这个变量时指向的都是同一个对象
synchronized(MultiThread.class){
System.out.println("enter thread1 ...");
System.out.println("thread1 is waiting");
try{
//释放锁有两种方式:(1)程序自然离开监视器的范围,即离开synchronized关键字管辖的代码范围
//(2)在synchronized关键字管辖的代码内部调用监视器对象的wait()方法。这里使用wait方法
MultiThread.class.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("thread1 is going on ...");
System.out.println("thread1 is being over!");
}
}
}
private static class Thread2 implements Runnable{
@Override
public void run() {
//notify方法并不释放锁,即使thread2调用了下面的sleep方法休息10ms,但thread1仍然不会执行
//因为thread2没有释放锁,所以Thread1得不到锁而无法执行
synchronized(MultiThread.class){
System.out.println("enter thread2 ...");
System.out.println("thread2 notify other thread can release wait status ...");
MultiThread.class.notify();
System.out.println("thread2 is sleeping ten millisecond ...");
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("thread2 is going on ...");
System.out.println("thread2 is being over!");
}
}
}
public static void main(String[] args) {
new Thread(new Thread1()).start();
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
new Thread(new Thread2()).start();
}
}
program results as shown in the figure below
Read More:
- Make DOS window wait and not flash back in VS
- How to solve the problem of no sound after Apple Mac recovers from sleep?
- Implementation of multithread sequential alternate execution by using lock in Java
- Lock wait timeout exceeded — transaction and index
- The usage and difference of atoi() and stoi() functions in C + +
- MySQL error 1205 (HY000): lock wait timeout exceeded; try restarting transaction
- Difference between isempty method and isblank method in stringutils
- The difference of four kinds of integer function (fix floor ceil round) in MATLAB
- Server (for example: HTTP) has a large number of time_ Solutions to wait
- The difference between classnotfoundexception and NoClassDefFoundError
- In C + + cin.getline The difference between () and getline () functions
- C#: Analysis of the difference between write() and writeline()
- [338] MySQL error: error 1205: lock wait timeout exceeded solution
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- ERROR: You must wait longer to change your password passwd: Authentication token manipulation error
- The difference between single equal sign and double equal sign EQ in shell script
- Difference between vs code user and system version
- Error — failed to start docker container, error iptables — wait – t nat
- Docker starts the image and reports an error. Iptables failed: iptables — wait – t NAT – a docker – P TCP
- The difference between problem and observables