public class WaitNotify {
static Object lock = new Object();
static boolean flag = false;
public static void main(String[] args) {
new Thread(new WaitThread(), "WaitThread").start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new NotifyThread(), "NotifyThread").start();
}
/**
* 流水线A,完成主要任务
*/
static class WaitThread implements Runnable{
@Override
public void run() {
// 获取object对象锁
synchronized (lock){
// 条件不满足时一直在等,等另外的线程改变该条件,并通知该wait线程
while (!flag){
try {
System.out.println(Thread.currentThread() + " is waiting, flag is "+flag);
// wait()方法调用就会释放锁,当前线程进入等待队列。
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// TODO 条件已经满足,不继续while,完成任务
System.out.println(Thread.currentThread() + " is running, flag is "+flag);
}
}
}
/**
* 流水线B,对开关进行控制,并通知流水线A
*/
static class NotifyThread implements Runnable{
@Override
public void run() {
// 获取等wait线程同一个object对象锁
synchronized (lock){
flag = true;
// 通知wait线程,我已经改变了条件,你可以继续返回执行了(返回之后继续判断while)
// 但是此时通知notify()操作并立即不会释放锁,而是要等当前线程释放锁
// TODO 我准备好配件了,我需要通知全部的组装流水线A.....
lock.notifyAll();
System.out.println(Thread.currentThread() + " hold lock, notify waitThread and flag is "+flag);
}
}
}
}
运行main函数,输出:
Thread[WaitThread,5,main] is waiting, flag is false
Thread[NotifyThread,5,main] hold lock, notify waitThread and flag is true
Thread[WaitThread,5,main] is running, flag is true
public class WaitNotify {
static Object lock = new Object();
static boolean flag = false;
public static void main(String[] args) {
new Thread(new WaitThread(), "WaitThread").start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new NotifyThread(), "NotifyThread").start();
}
/**
* 流水线A,完成主要任务
*/
static class WaitThread implements Runnable{
@Override
public void run() {
// 获取object对象锁
synchronized (lock){
// 条件不满足时一直在等,等另外的线程改变该条件,并通知该wait线程
while (!flag){
try {
System.out.println(Thread.currentThread() + " is waiting, flag is "+flag);
// wait()方法调用就会释放锁,当前线程进入等待队列。
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// TODO 条件已经满足,不继续while,完成任务
System.out.println(Thread.currentThread() + " is running, flag is "+flag);
}
}
}
/**
* 流水线B,对开关进行控制,并通知流水线A
*/
static class NotifyThread implements Runnable{
@Override
public void run() {
// 获取等wait线程同一个object对象锁
synchronized (lock){
flag = true;
// 通知wait线程,我已经改变了条件,你可以继续返回执行了(返回之后继续判断while)
// 但是此时通知notify()操作并立即不会释放锁,而是要等当前线程释放锁
// TODO 我准备好配件了,我需要通知全部的组装流水线A.....
lock.notifyAll();
System.out.println(Thread.currentThread() + " hold lock, notify waitThread and flag is "+flag);
}
// 模拟跟流水线B竞争
synchronized (lock){
System.out.println(Thread.currentThread() + " hold lock again");
}
}
}
}
输出结果:
Thread[WaitThread,5,main] is waiting, flag is false
Thread[NotifyThread,5,main] hold lock, notify waitThread and flag is true
Thread[NotifyThread,5,main] hold lock again
Thread[WaitThread,5,main] is running, flag is true
Thread[WaitThread,5,main] is waiting, flag is false
Thread[NotifyThread,5,main] hold lock, notify waitThread and flag is true
Thread[WaitThread,5,main] is running, flag is true
Thread[NotifyThread,5,main] hold lock again
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
// 这里的while(){wait(millis)} 就是利用等待/通知中的等待模式,只不过加上了超时设置
if (millis == 0) {
// while循环,当线程还活着的时候就一直循环等待,直到线程终止
while (isAlive()) {
// wait等待
wait(0);
}
// 条件满足时返回
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
public class WaitNotify {
static Object lock = new Object();
static boolean flag = false;
public static void main(String[] args) {
new Thread(new WaitThread(), "WaitThread").start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new NotifyThread(), "NotifyThread").start();
}
/**
* 流水线A,完成主要任务
*/
static class WaitThread implements Runnable{
@Override
public void run() {
// 获取object对象锁
synchronized (lock){
// 条件不满足时一直在等,等另外的线程改变该条件,并通知该wait线程
while (!flag){
try {
System.out.println(Thread.currentThread() + " is waiting, flag is "+flag);
// wait()方法调用就会释放锁,当前线程进入等待队列。
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// TODO 条件已经满足,不继续while,完成任务
System.out.println(Thread.currentThread() + " is running, flag is "+flag);
}
}
}
/**
* 流水线B,对开关进行控制,并通知流水线A
*/
static class NotifyThread implements Runnable{
@Override
public void run() {
// 获取等wait线程同一个object对象锁
synchronized (lock){
flag = true;
// 通知wait线程,我已经改变了条件,你可以继续返回执行了(返回之后继续判断while)
// 但是此时通知notify()操作并立即不会释放锁,而是要等当前线程释放锁
// TODO 我准备好配件了,我需要通知全部的组装流水线A.....
lock.notifyAll();
System.out.println(Thread.currentThread() + " hold lock, notify waitThread and flag is "+flag);
}
}
}
}
运行main函数,输出:
Thread[WaitThread,5,main] is waiting, flag is false
Thread[NotifyThread,5,main] hold lock, notify waitThread and flag is true
Thread[WaitThread,5,main] is running, flag is true
public class WaitNotify {
static Object lock = new Object();
static boolean flag = false;
public static void main(String[] args) {
new Thread(new WaitThread(), "WaitThread").start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new NotifyThread(), "NotifyThread").start();
}
/**
* 流水线A,完成主要任务
*/
static class WaitThread implements Runnable{
@Override
public void run() {
// 获取object对象锁
synchronized (lock){
// 条件不满足时一直在等,等另外的线程改变该条件,并通知该wait线程
while (!flag){
try {
System.out.println(Thread.currentThread() + " is waiting, flag is "+flag);
// wait()方法调用就会释放锁,当前线程进入等待队列。
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// TODO 条件已经满足,不继续while,完成任务
System.out.println(Thread.currentThread() + " is running, flag is "+flag);
}
}
}
/**
* 流水线B,对开关进行控制,并通知流水线A
*/
static class NotifyThread implements Runnable{
@Override
public void run() {
// 获取等wait线程同一个object对象锁
synchronized (lock){
flag = true;
// 通知wait线程,我已经改变了条件,你可以继续返回执行了(返回之后继续判断while)
// 但是此时通知notify()操作并立即不会释放锁,而是要等当前线程释放锁
// TODO 我准备好配件了,我需要通知全部的组装流水线A.....
lock.notifyAll();
System.out.println(Thread.currentThread() + " hold lock, notify waitThread and flag is "+flag);
}
// 模拟跟流水线B竞争
synchronized (lock){
System.out.println(Thread.currentThread() + " hold lock again");
}
}
}
}
输出结果:
Thread[WaitThread,5,main] is waiting, flag is false
Thread[NotifyThread,5,main] hold lock, notify waitThread and flag is true
Thread[NotifyThread,5,main] hold lock again
Thread[WaitThread,5,main] is running, flag is true
Thread[WaitThread,5,main] is waiting, flag is false
Thread[NotifyThread,5,main] hold lock, notify waitThread and flag is true
Thread[WaitThread,5,main] is running, flag is true
Thread[NotifyThread,5,main] hold lock again
public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
// 这里的while(){wait(millis)} 就是利用等待/通知中的等待模式,只不过加上了超时设置
if (millis == 0) {
// while循环,当线程还活着的时候就一直循环等待,直到线程终止
while (isAlive()) {
// wait等待
wait(0);
}
// 条件满足时返回
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}