synchronized 是非公平锁。ReentrantLock 默认是非公平锁,但可设置为公平锁。
那线程通过Object.nofity()
和 Condition.signal()
被唤醒时是否是公平的呢?
先说结果,在Java 1.8 HotSpot
下,两者都是公平的。
查看Java文档:
void signal()
Wakes up one waiting thread.
If any threads are waiting on this condition then one is selected for waking up. That thread must then re-acquire the lock before returning from await.
Implementation Considerations
An implementation may (and typically does) require that the current thread hold the lock associated with this Condition when this method is called. Implementations must document this precondition and any actions taken if the lock is not held. Typically, an exception such as IllegalMonitorStateException will be thrown.
public final void 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 wait methods.
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.
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:
- By executing a synchronized instance method of that object.
- By executing the body of a synchronized statement that synchronizes on the object.
- For objects of type Class, by executing a synchronized static method of that class.
Only one thread at a time can own an object's monitor.
Object.nofity()
的文档明确说一个随机的线程将被唤醒,但具体情况将由实现者决定,因为Object.nofity()
是一个native方法。
Condition.signal()
的文档则说一个被选定的线程将被唤醒。
notify()
方法进行实验:public class NotifyTest {
private final Object object = new Object();
private List<Integer> sleepList = new LinkedList<>();
private List<Integer> notifyList = new LinkedList<>();
public void startThread(int i) {
new Thread(new Runnable() { // 启动一个线程
@Override
public void run() {
synchronized (object) {
try {
sleepList.add(i);
object.wait();
notifyList.add(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void main(String[] args) throws InterruptedException {
NotifyTest a = new NotifyTest();
for(int i = 1; i < 30; i++){
a.startThread(i);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println();
for(int i = 1; i < 30; i++){
Thread.sleep(10);
synchronized (a.object) {
a.object.notify();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("休眠顺序" + a.sleepList);
System.out.println("唤醒顺序" + a.notifyList);
}
}
休眠顺序[1, 4, 8, 12, 16, 3, 7, 11, 15, 19, 23, 27, 5, 9, 13, 17, 21, 25, 29, 20, 24, 28, 2, 6, 10, 14, 18, 22, 26]
唤醒顺序[1, 4, 8, 12, 16, 3, 7, 11, 15, 19, 23, 27, 5, 9, 13, 17, 21, 25, 29, 20, 24, 28, 2, 6, 10, 14, 18, 22, 26]
可以清晰的看到notify()
方法是按先进先出的顺序唤醒的,即是公平的。本实验在Java 1.8 HotSpot
下测试,可以看出Java 1.8 HotSpot
把notify()
实现为公平的方式。
signal()
方法进行实验:public class SignalTest {
private static ReentrantLock lock = new ReentrantLock();
private static Condition condition = lock.newCondition();
private List<Integer> sleepList = new LinkedList<>();
private List<Integer> notifyList = new LinkedList<>();
public void startThread(int i) {
new Thread(new Runnable() {
@Override
public void run() {
try {
lock.lock();
sleepList.add(i);
condition.await();
notifyList.add(i);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}).start();
}
public static void main(String[] args) throws InterruptedException {
SignalTest a = new SignalTest();
for(int i = 1; i < 30; i++){
a.startThread(i);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println();
for(int i = 1; i < 30; i++){
Thread.sleep(10);
lock.lock();
condition.signal(); // signal() 需要获得lock
lock.unlock();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("休眠顺序" + a.sleepList);
System.out.println("唤醒顺序" + a.notifyList);
}
}
休眠顺序[2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, 18, 19, 20, 22, 23, 24, 27, 1, 13, 21, 25, 29, 5, 9, 17, 28, 26]
唤醒顺序[2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, 18, 19, 20, 22, 23, 24, 27, 1, 13, 21, 25, 29, 5, 9, 17, 28, 26]
可以看到signal()
方法也是公平的。