notify()和notifyAll()以及wait()方法用于线程间的通信。通过调用wait()方法进入WaitSet的线程会一直处于WAITING状态,直到任何其他的线程在同一锁的对象上调用notify()或者notify()方法。 限制的问题是,notify()和notifyAll()方法都是用来向处于WAITING状态的线程发送通知的,那么他们之间有什么区别,或者我们应该在哪使用notify()或者notifyAll方法? 让我们先来看看notify的行文:
class Geek1 extends Thread {
@Override
public void run() {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + "...starts");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "...notified");
}
}
}
class Geek2 extends Thread {
Geek1 geeks1;
Geek2(Geek1 geeks1) {
this.geeks1 = geeks1;
}
@Override
public void run() {
synchronized (this.geeks1) {
System.out.println
(Thread.currentThread().getName() + "...starts");
try {
this.geeks1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println
(Thread.currentThread().getName() + "...notified");
}
}
}
class Geek3 extends Thread {
Geek1 geeks1;
Geek3(Geek1 geeks1) {
this.geeks1 = geeks1;
}
@Override
public void run() {
synchronized (this.geeks1) {
System.out.println
(Thread.currentThread().getName() + "...starts");
this.geeks1.notify();
System.out.println
(Thread.currentThread().getName() + "...notified");
}
}
}
class MainClass {
public static void main(String[] args) throws InterruptedException {
Geek1 geeks1 = new Geek1();
Geek2 geeks2 = new Geek2(geeks1);
Geek3 geeks3 = new Geek3(geeks1);
Thread t1 = new Thread(geeks1, "Thread-1");
Thread t2 = new Thread(geeks2, "Thread-2");
Thread t3 = new Thread(geeks3, "Thread-3");
t1.start();
t2.start();
Thread.sleep(100);
t3.start();
}
}
上述代码执行结果如下:
Thread-1...starts
Thread-2...starts
Thread-3...starts
Thread-3...notified
Thread-1...notified
我们再看看notifyAll的行为:
// Java program to illustrate the
// behavior of notifyAll() method
class Geek1 extends Thread {
public void run()
{
synchronized(this)
{
System.out.println
(Thread.currentThread().getName() + "...starts");
try {
this.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println
(Thread.currentThread().getName() + "...notified");
}
}
} class Geek2 extends Thread {
Geek1 geeks1;
Geek2(Geek1 geeks1)
{
this.geeks1 = geeks1;
}
public void run()
{
synchronized(this.geeks1)
{
System.out.println
(Thread.currentThread().getName() + "...starts");
try {
this.geeks1.wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println
(Thread.currentThread().getName() + "...notified");
}
}
} class Geek3 extends Thread {
Geek1 geeks1;
Geek3(Geek1 geeks1)
{
this.geeks1 = geeks1;
}
public void run()
{
synchronized(this.geeks1)
{
System.out.println
(Thread.currentThread().getName() + "...starts");
this.geeks1.notifyAll();
System.out.println
(Thread.currentThread().getName() + "...notified");
}
}
} class MainClass {
public static void main(String[] args) throws InterruptedException
{
Geek1 geeks1 = new Geek1();
Geek2 geeks2 = new Geek2(geeks1);
Geek3 geeks3 = new Geek3(geeks1);
Thread t1 = new Thread(geeks1, "Thread-1");
Thread t2 = new Thread(geeks2, "Thread-2");
Thread t3 = new Thread(geeks3, "Thread-3");
t1.start();
t2.start();
Thread.sleep(100);
t3.start();
}
}
执行结果:
Thread-1...starts
Thread-2...starts
Thread-3...starts
Thread-3...notified
Thread-2...notified
Thread-1...notified
参考https://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again