欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

wait/notify/notifyAll在Object类中

发布时间:2025/3/21 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 wait/notify/notifyAll在Object类中 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

wait/notify/notifyAll在Object类中
因为我们在使用synchronized锁 对象锁可以是任意对象,所以wait/notify/notifyAll需要放在Object类中。
wait/notify/简单的用法
public class Thread03 extends Thread {
@Override
public void run() {
try {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + “>>当前线程阻塞,同时释放锁!<<”);
this.wait();
}
System.out.println(">>run()<<");
} catch (InterruptedException e) {

} }public static void main(String[] args) {Thread03 thread = new Thread03();thread.start();try {Thread.sleep(3000);} catch (Exception e) {}synchronized (thread) {// 唤醒正在阻塞的线程thread.notify();}}

}

多线程通讯实现生产者与消费者

public class Thread04 {
class Res {
/**
* 姓名
/
private String userName;
/*
* 性别
/
private char sex;
/*
* 标记
*/
private boolean flag = false;
}

class InputThread extends Thread {private Res res;public InputThread(Res res) {this.res = res;}@Overridepublic void run() {int count = 0;while (true) {synchronized (res) {//flag = false 写入输入 flag = true 则不能写入数据 只能读取数据try {// 如果flag = true 则不能写入数据 只能读取数据 同时释放锁!if (res.flag) {res.wait();}} catch (Exception e) {}if (count == 0) {this.res.userName = "余胜军";this.res.sex = '男';} else {this.res.userName = "小薇";this.res.sex = '女';}res.flag = true;res.notify();}count = (count + 1) % 2;}} }class OutThread extends Thread {private Res res;public OutThread(Res res) {this.res = res;}@Overridepublic void run() {while (true) {synchronized (res) {try {if (!res.flag) {res.wait();}} catch (Exception e) {}System.out.println(res.userName + "," + res.sex);res.flag = false;res.notify();}}} }public static void main(String[] args) {new Thread04().print(); }public void print() {Res res = new Res();InputThread inputThread = new InputThread(res);OutThread outThread = new OutThread(res);inputThread.start();outThread.start(); }

}

/**

  • flag 默认值==false
  • flag false 输入线程 输入值 输出线程 先拿到锁 释放锁
  • flag true 输出线程 输出值
    */
    public boolean flag = false;

Join/Wait与sleep之间的区别
sleep(long)方法在睡眠时不释放对象锁
join(long)方法先执行另外的一个线程,在等待的过程中释放对象锁 底层是基于wait封装的,
Wait(long)方法在等待的过程中释放对象锁
三个线程 T1,T2,T3,怎么确保它们按顺序执行?
Thread t1 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,线程执行”), “t1”);
Thread t2 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,线程执行”), “t2”);
Thread t3 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,线程执行”), “t3”);
t1.start();
t2.start();
t3.start();

public class Thread05 {

public static void main(String[] args) throws InterruptedException {Thread t1 = new Thread(() -> {try {Thread.sleep(3000);} catch (Exception e) {}System.out.println(Thread.currentThread().getName() + ",线程执行");}, "t1");Thread t2 = new Thread(() -> {try {t1.join();} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName() + ",线程执行");}, "t2");Thread t3 = new Thread(() -> {try {t2.join();} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName() + ",线程执行");}, "t3");t1.start();t2.start();t3.start(); }

}

总结

以上是生活随笔为你收集整理的wait/notify/notifyAll在Object类中的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。