欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java通過(guò)wait()和notifyAll()方法實(shí)現(xiàn)線程間通信

 更新時(shí)間:2017年04月10日 09:04:50   作者:FrankYou  
這篇文章主要為大家詳細(xì)介紹了Java通過(guò)wait()和notifyAll()方法實(shí)現(xiàn)線程間通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)線程間通信的具體代碼,供大家參考,具體內(nèi)容如下

Java代碼(使用了2個(gè)內(nèi)部類):

package Threads;

import java.util.LinkedList;

/**
 * Created by Frank
 */
public class ProdCons {
 protected LinkedList<Object> list = new LinkedList<>();
 protected int max;
 protected boolean done = false;

 public static void main(String[] args) throws InterruptedException {
  ProdCons prodCons = new ProdCons(100, 3, 4);
  Thread.sleep(5 * 1000);
  synchronized (prodCons.list) {
   prodCons.done = true;
   try {
    prodCons.notifyAll();
   } catch (Exception ex) {
   }
  }
 }

 private ProdCons(int maxThreads, int nP, int nC) {
  this.max = maxThreads;
  for (int i = 0; i < nP; i++) {
   new Producer().start();
  }
  for (int i = 0; i < nC; i++) {
   new Consumer().start();
  }
 }

 class Producer extends Thread {
  public void run() {
   while (true) {
    Object justProduced = null;
    try {
     justProduced = getObj();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    synchronized (list) {
     while (list.size() == max) {
      try {
       list.wait();
      } catch (InterruptedException e) {
       System.out.println("Producer INTERRUPTED");
      }
     }
     list.addFirst(justProduced);
     list.notifyAll();
     System.out.println("Produced 1;List size now " + list.size());
     if (done) {
      break;
     }
    }
   }
  }
 }

 class Consumer extends Thread {
  public void run() {
   while (true) {
    Object object = null;
    synchronized (list) {
     if (list.size() == 0) {
      try {
       list.wait();
      } catch (InterruptedException e) {
       System.out.println("Consumer INTERRUPTED");
      }
     }
     if (list.size() > 0) {
      object = list.removeLast();
     }
     list.notifyAll();
     System.out.println("List size now " + list.size());
     if (done) {
      break;
     }
    }
    if (null != object) {
     System.out.println("Consuming object " + object);
    }
   }
  }
 }

 private Object getObj() throws InterruptedException {
  Thread.sleep(1000);
  return new Object();
 }
}

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

相關(guān)文章

  • Java編程實(shí)現(xiàn)排他鎖代碼詳解

    Java編程實(shí)現(xiàn)排他鎖代碼詳解

    這篇文章主要介紹了Java編程實(shí)現(xiàn)排他鎖的相關(guān)內(nèi)容,敘述了實(shí)現(xiàn)此代碼鎖所需要的功能,以及作者的解決方案,然后向大家分享了設(shè)計(jì)源碼,需要的朋友可以參考下。
    2017-10-10
  • 一文詳解RabbitMQ如何保證消息可靠性

    一文詳解RabbitMQ如何保證消息可靠性

    這篇文章將詳細(xì)介紹RabbitMQ的消息可靠性機(jī)制,如消息丟失,消息重復(fù)性消費(fèi),消息積壓等問(wèn)題,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼

    Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼

    這篇文章主要介紹了Java流操作之?dāng)?shù)據(jù)流實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Java中支持可變參數(shù)詳解

    Java中支持可變參數(shù)詳解

    那個(gè)可變參數(shù)的就是個(gè)數(shù)組,你傳多少個(gè)參數(shù)都被放到那個(gè)數(shù)組里面。這樣方便了程序員,因?yàn)槿绻淮_定要傳的參數(shù)的個(gè)數(shù)的話,我們要寫帶1個(gè)參數(shù)的,帶2個(gè)參數(shù),帶3個(gè)參數(shù)的,這樣很麻煩。 該進(jìn)后的這個(gè)方法,我們只要寫一個(gè)函數(shù)就好,可以傳任意個(gè)參數(shù)。
    2015-05-05
  • SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值

    SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)yml配置文件為變量賦值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 關(guān)于mybatis-plus-generator的簡(jiǎn)單使用示例詳解

    關(guān)于mybatis-plus-generator的簡(jiǎn)單使用示例詳解

    在springboot項(xiàng)目中集成mybatis-plus是很方便開發(fā)的,最近看了一下plus的文檔,簡(jiǎn)單用一下它的代碼生成器,接下來(lái)通過(guò)實(shí)例代碼講解關(guān)于mybatis-plus-generator的簡(jiǎn)單使用,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • 基于SpringBoot實(shí)現(xiàn)自定義插件的流程詳解

    基于SpringBoot實(shí)現(xiàn)自定義插件的流程詳解

    在SpringBoot中,插件是一種擴(kuò)展機(jī)制,它可以幫助我們?cè)趹?yīng)用程序中快速地添加一些額外的功能,在本文中,我們將介紹如何使用 SpringBoot實(shí)現(xiàn)自定義插件,需要的朋友可以參考下
    2023-06-06
  • springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析

    springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析

    這篇文章主要介紹了springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java調(diào)用mysql存儲(chǔ)過(guò)程實(shí)例分析

    java調(diào)用mysql存儲(chǔ)過(guò)程實(shí)例分析

    這篇文章主要介紹了java調(diào)用mysql存儲(chǔ)過(guò)程的方法,以實(shí)例形式較為詳細(xì)的分析了mysql數(shù)據(jù)庫(kù)的建立和存儲(chǔ)過(guò)程的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-06-06
  • Java Spring @Lazy延遲注入源碼案例詳解

    Java Spring @Lazy延遲注入源碼案例詳解

    這篇文章主要介紹了Java Spring @Lazy延遲注入源碼案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評(píng)論