日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

wait/notify/notifyAll在Object类中

發(fā)布時間:2025/3/21 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 wait/notify/notifyAll在Object类中 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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() + “>>當(dāng)前線程阻塞,同時釋放鎖!<<”);
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();}}

}

多線程通訊實現(xiàn)生產(chǎn)者與消費(fèi)者

public class Thread04 {
class Res {
/**
* 姓名
/
private String userName;
/*
* 性別
/
private char sex;
/*
* 標(biāo)記
*/
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 則不能寫入數(shù)據(jù) 只能讀取數(shù)據(jù)try {// 如果flag = true 則不能寫入數(shù)據(jù) 只能讀取數(shù)據(jù) 同時釋放鎖!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 默認(rèn)值==false
  • flag false 輸入線程 輸入值 輸出線程 先拿到鎖 釋放鎖
  • flag true 輸出線程 輸出值
    */
    public boolean flag = false;

Join/Wait與sleep之間的區(qū)別
sleep(long)方法在睡眠時不釋放對象鎖
join(long)方法先執(zhí)行另外的一個線程,在等待的過程中釋放對象鎖 底層是基于wait封裝的,
Wait(long)方法在等待的過程中釋放對象鎖
三個線程 T1,T2,T3,怎么確保它們按順序執(zhí)行?
Thread t1 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,線程執(zhí)行”), “t1”);
Thread t2 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,線程執(zhí)行”), “t2”);
Thread t3 = new Thread(() -> System.out.println(Thread.currentThread().getName() + “,線程執(zhí)行”), “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() + ",線程執(zhí)行");}, "t1");Thread t2 = new Thread(() -> {try {t1.join();} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName() + ",線程執(zhí)行");}, "t2");Thread t3 = new Thread(() -> {try {t2.join();} catch (InterruptedException e) {}System.out.println(Thread.currentThread().getName() + ",線程執(zhí)行");}, "t3");t1.start();t2.start();t3.start(); }

}

總結(jié)

以上是生活随笔為你收集整理的wait/notify/notifyAll在Object类中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。