标签:其他 对象 ace port over cat package 一个 interrupt
Object对象提供了wait( ) 方法和 notify ( ) 方法。
wait( )方法需要配对着synchronized 关键字使用。
当一个线程的资源调用 wait( )方法时,它会失去锁,然后进入等待序列。
当其他线程调用notify方法时才会重新激活。
package com.xm.thread.t_19_01_27;
import java.util.concurrent.TimeUnit;
public class NotifyAndWait{
private static Object object = new Object();
static class WaitThread extends Thread{
@Override
public void run() {
System.out.println("WaitThread Start!");
try {
System.out.println("Object wait!!!");
synchronized (object) {
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("WaitThread Finish!");
}
}
static class NotifyThread extends Thread{
@Override
public void run() {
System.out.println("NotifyThread Start!");
System.out.println("Object notify");
synchronized (object) {
object.notify();
}
System.out.println("NotifyThread Finish!");
}
}
public static void main(String[] args) throws InterruptedException {
new WaitThread().start();
TimeUnit.SECONDS.sleep(5);
new NotifyThread().start();
}
}
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
notify( )方法只会随机激活等待序列中的一个线程,而notifyAll( )方法激活所有等待的线程。
package com.xm.thread.t_19_01_27;
import java.util.concurrent.TimeUnit;
public class NotifyAndWait{
private static Object object = new Object();
static class WaitThread extends Thread{
@Override
public void run() {
System.out.println("WaitThread Start!");
try {
System.out.println("Object wait!!!");
synchronized (object) {
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("WaitThread Finish!");
}
}
static class NotifyThread extends Thread{
@Override
public void run() {
System.out.println("NotifyThread Start!");
System.out.println("Object notify");
synchronized (object) {
object.notify();
}
System.out.println("NotifyThread Finish!");
}
}
public static void main(String[] args) throws InterruptedException {
new WaitThread().start();
new WaitThread().start();
TimeUnit.SECONDS.sleep(5);
new NotifyThread().start();
}
}
WaitThread Start!
Object wait!!!
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
两个WaitThread线程只被唤醒了一个。
package com.xm.thread.t_19_01_27;
import java.util.concurrent.TimeUnit;
public class NotifyAndWait{
private static Object object = new Object();
static class WaitThread extends Thread{
@Override
public void run() {
System.out.println("WaitThread Start!");
try {
System.out.println("Object wait!!!");
synchronized (object) {
object.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("WaitThread Finish!");
}
}
static class NotifyThread extends Thread{
@Override
public void run() {
System.out.println("NotifyThread Start!");
System.out.println("Object notify");
synchronized (object) {
object.notifyAll();
}
System.out.println("NotifyThread Finish!");
}
}
public static void main(String[] args) throws InterruptedException {
new WaitThread().start();
new WaitThread().start();
TimeUnit.SECONDS.sleep(5);
new NotifyThread().start();
}
}
WaitThread Start!
Object wait!!!
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
WaitThread Finish!
notifyAll( )方法会唤醒处在等待序列中的所有线程。
标签:其他 对象 ace port over cat package 一个 interrupt
原文地址:https://www.cnblogs.com/TimerHotel/p/thread04.html