Thread 类常用的方法与Object类提供的线程操作方法:(一个对象只有一把锁🔒)
一. Wait :
java document for Object wait();
/** * Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object. * In other words, this method behaves exactly as if it simply * performs the call {@code wait(0)}. * <p>
Translation:
使当前的线程去等待,直到其他的线程调用了这个对象的notify 和notifyall 方法
换句话说,这个wait 方法的行为与调用了wait(0)方法一样的
* The current thread must own this object's monitor. The thread * releases ownership of this monitor and waits until another thread * notifies threads waiting on this object's monitor to wake up * either through a call to the {@code notify} method or the * {@code notifyAll} method. The thread then waits until it can * re-obtain ownership of the monitor and resumes execution. * <p>
Translation:关于面试的知识点,其实在document 里已经明确的说明了,as fellows:
当前的线程必须有这个对象的监控即锁,这个线程会释放掉这个锁的拥有权
直到其他线程通过执行notify 与notify all 方法去唤醒它,
然后这个线程会继续等待去再次获取这个锁的拥有权继续执行;
* As in the one argument version, interrupts and spurious wakeups are * possible, and this method should always be used in a loop: * <pre> * synchronized (obj) { * while (<condition does not hold>) * obj.wait(); * ... // Perform action appropriate to condition * } * </pre> * This method should only be called by a thread that is the owner * of this object's monitor. See the {@code notify} method for a * description of the ways in which a thread can become the owner of * a monitor.
Translation:
这个方法只能被拥有这个对象的锁的线程调用,在notify 方法中有关于描述一个线程拥有锁的方式;
通过源码解读,可以总结出wait() 方法的特点:
1.使用wait() 方法时,我们必须拿到当前对象的监控(锁),即在synchronized 里面执行才能拿到对象锁
2.wait()方法会释放掉当前对象锁的所有权,这么理解吧,一释放其他的线程就可以执行synchronized 方法,
3.只有通过notify 与notifyall 方法才可以唤醒wait 方法的线程,唤醒之后并不是立马拿到当前对象的锁,而是等待去re-obtain ownership
二. notify
java document for notify()
/**
* Wakes up a single thread that is waiting on this object's
* monitor. If any threads are waiting on this object, one of them
* is chosen to be awakened. The choice is arbitrary and occurs at
* the discretion of the implementation. A thread waits on an object's
* monitor by calling one of the {@code wait} methods.
* <p>
Translation:
唤醒一个正在等待当前对象锁🔒的一个线程,如果一些线程在这个对象上等待,他会选择他们
其中的一个去唤醒,这个选择上随机的由实现决定的。通过调用 wait 方法让一个线程等待对象
的锁。
* The awakened thread will not be able to proceed until the current
* thread relinquishes the lock on this object. The awakened thread will
* compete in the usual manner with any other threads that might be
* actively competing to synchronize on this object; for example, the
* awakened thread enjoys no reliable privilege or disadvantage in being
* the next thread to lock this object.
* <p>
Translation:
唤醒的线程不能够去执行,直到当前的线程放弃了这个对象上的锁🔒,唤醒的线程将会和其他的线程去竞争
执行这个对象的synchronized 方法,
* This method should only be called by a thread that is the owner
* of this object's monitor. A thread becomes the owner of the
* object's monitor in one of three ways:
* <ul>
* <li>By executing a synchronized instance method of that object.
* <li>By executing the body of a {@code synchronized} statement
* that synchronizes on the object.
* <li>For objects of type {@code Class,} by executing a
* synchronized static method of that class.
* </ul>
* <p>
Translation:
与wait 方法一样,这个方法只能被拥有这个对象的锁的线程调用,一个线程成为拥有对象锁的三种方式:
1.在这个对象运行synchronized 实例
2.在这个对象里执行一个synchronized 代码块
3.关于class 对象,执行一个静态的synchronized 方法;
* Only one thread at a time can own an object's monitor.
通过源码解读,可以总结出notify() 方法的特点:
1.使用notify() 方法时,我们必须拿到当前对象的监控(锁),即在synchronized 里面执行才能拿到对象锁
2.notify 方法,只会arbitrary一个等待的线程去唤醒。
三. sleep()
java document for thread sleep() /** * Causes the currently executing thread to sleep (temporarily cease * execution) for the specified number of milliseconds, subject to * the precision and accuracy of system timers and schedulers. The thread * does not lose ownership of any monitors.
**/
Translation:
使当前的执行线程去睡眠(暂时的停止执行)在特定的时间毫秒内,受系统定时器和调度器精度的影响
这个线程不会丢弃这个锁的所有权,其他线程锁无法去访问的。
通过源码解读,可以总结出notify() 方法的特点:
1.sleep() 使当前的线程在指定时间内暂停执行,但他不会丢弃对象的锁,这也是和wait 的区别,其他线程是无法访问的。