亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談java多線程wait,notify

 更新時(shí)間:2019年05月29日 10:15:10   作者:lijingyulee  
這篇文章主要介紹了java多線程wait,notify,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面小編和大家一起來(lái)學(xué)習(xí)一下吧

前言

1.因?yàn)樯婕暗綄?duì)象鎖,Wait、Notify一定要在synchronized里面進(jìn)行使用。
2.Wait必須暫定當(dāng)前正在執(zhí)行的線程,并釋放資源鎖,讓其他線程可以有機(jī)會(huì)運(yùn)行
3.notify/notifyall: 喚醒線程

共享變量

public class ShareEntity {
private String name;
// 線程通訊標(biāo)識(shí)
private Boolean flag = false;
public ShareEntity() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
}

線程1(生產(chǎn)者)

public class CommunicationThread1 extends Thread{
private ShareEntity shareEntity;
public CommunicationThread1(ShareEntity shareEntity) {
this.shareEntity = shareEntity;
}
@Override
public void run() {
int num = 0;
while (true) {
synchronized (shareEntity) {
if (shareEntity.getFlag()) {
try {
shareEntity.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (num % 2 == 0)
shareEntity.setName("thread1-set-name-0");
else
shareEntity.setName("thread1-set-name-1");
num++;
shareEntity.setFlag(true);
shareEntity.notify();
}
}
}
}

線程2(消費(fèi)者)

public class CommunicationThread2 extends Thread{
private ShareEntity shareEntity;
public CommunicationThread2(ShareEntity shareEntity) {
this.shareEntity = shareEntity;
}
@Override
public void run() {
while (true) {
synchronized (shareEntity) {
if (!shareEntity.getFlag()) {
try {
shareEntity.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(shareEntity.getName());
shareEntity.setFlag(false);
shareEntity.notify();
}
}
}
}

請(qǐng)求

@RequestMapping("test-communication")
public void testCommunication() {
ShareEntity shareEntity = new ShareEntity();
CommunicationThread1 thread1 = new CommunicationThread1(shareEntity);
CommunicationThread2 thread2 = new CommunicationThread2(shareEntity);
thread1.start();
thread2.start();
}

結(jié)果

thread1-set-name-0
thread1-set-name-1
thread1-set-name-0
thread1-set-name-1
thread1-set-name-0
thread1-set-name-1
thread1-set-name-0

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑

    詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑

    本文主要介紹了springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能

    Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能

    在早期的網(wǎng)絡(luò)編程中,Socket是很常見的實(shí)現(xiàn)技術(shù)之一,比如早期的聊天室,就是基于這種技術(shù)進(jìn)行實(shí)現(xiàn)的,另外現(xiàn)在有些消息推送,也可以基于Socket實(shí)現(xiàn),本文小編給大家介紹了Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能的示例,需要的朋友可以參考下
    2023-11-11
  • Java for循環(huán)的幾種用法分析

    Java for循環(huán)的幾種用法分析

    本篇文章小編為大家介紹,Java for循環(huán)的幾種用法分析。需要的朋友參考下
    2013-04-04
  • 解析如何開發(fā)FineReport的自定義控件

    解析如何開發(fā)FineReport的自定義控件

    FineReport作為插件化開發(fā)的報(bào)表軟件,有些特殊需求的功能需要自己開發(fā),開發(fā)的插件包帆軟官方有提提供,可以去帆軟論壇上找,本文將主要介紹如何開發(fā)一個(gè)自定義控件,這里講講方法論。需要的朋友一起來(lái)看下吧
    2016-12-12
  • @PathVariable和@RequestParam傳參為空問題及解決

    @PathVariable和@RequestParam傳參為空問題及解決

    這篇文章主要介紹了@PathVariable和@RequestParam傳參為空問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot最簡(jiǎn)潔的國(guó)際化配置

    SpringBoot最簡(jiǎn)潔的國(guó)際化配置

    這篇文章主要介紹了SpringBoot最簡(jiǎn)潔的國(guó)際化配置,Spring Boot是一個(gè)用于構(gòu)建獨(dú)立的、生產(chǎn)級(jí)別的Spring應(yīng)用程序的框架,國(guó)際化是一個(gè)重要的功能,它允許應(yīng)用程序根據(jù)用戶的語(yǔ)言和地區(qū)顯示不同的內(nèi)容,在Spring Boot中,實(shí)現(xiàn)國(guó)際化非常簡(jiǎn)單,需要的朋友可以參考下
    2023-10-10
  • Spring+Http請(qǐng)求+HttpClient實(shí)現(xiàn)傳參

    Spring+Http請(qǐng)求+HttpClient實(shí)現(xiàn)傳參

    這篇文章主要介紹了Spring+Http請(qǐng)求+HttpClient實(shí)現(xiàn)傳參,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • C語(yǔ)言實(shí)現(xiàn)矩陣運(yùn)算案例詳解

    C語(yǔ)言實(shí)現(xiàn)矩陣運(yùn)算案例詳解

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)矩陣運(yùn)算案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • JVM要雙親委派的原因及如何打破它

    JVM要雙親委派的原因及如何打破它

    平時(shí)做業(yè)務(wù)開發(fā)比較少接觸類加載器,但是如果想深入學(xué)習(xí),了解類加載的原理是必不可少的.java的類加載器有哪些?什么是雙親委派?為什么要雙親委派?如何打破它?接下來(lái)本文就帶大家詳細(xì)介紹這些知識(shí) ,需要的朋友可以參考下
    2021-06-06
  • Java如何從List中刪除元素的正確用法舉例

    Java如何從List中刪除元素的正確用法舉例

    這篇文章主要給大家介紹了關(guān)于Java如何從List中刪除元素的正確用法, 列表List是Java中的一種數(shù)據(jù)結(jié)構(gòu),存放按照添加順序的元素,是個(gè)有序的集合,需要的朋友可以參考下
    2023-07-07

最新評(píng)論