JAVA中怎么使用synchronized、wait、notify實(shí)現(xiàn)同步通信

本篇文章為大家展示了JAVA中怎么使用synchronized、wait、notify實(shí)現(xiàn)同步通信,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供三元網(wǎng)站建設(shè)、三元做網(wǎng)站、三元網(wǎng)站設(shè)計(jì)、三元網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、三元企業(yè)網(wǎng)站模板建站服務(wù),10年三元做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

package com.study;

public class Demo {
  
  public static void main(String[] args) {
    Demo demo = new Demo();
    final OutPutClass putPutClass = demo.new OutPutClass();
    Thread thread = new Thread(new Runnable() {
      
      @Override
      public void run() {
        while(true){
          putPutClass.ins();
        }
      }
    });
    thread.start();
    
    Thread thread2 = new Thread(new Runnable() {
      
      @Override
      public void run() {
        while(true){
          putPutClass.des();
        }
      }
    });
    thread2.start();
  }
  
  class OutPutClass{
    private boolean isSync = true;
    public synchronized void ins(){
      try {
        while(!isSync){
          this.wait();
        }
        Thread.sleep(1000L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("正在上傳中....");
      isSync = false;
      this.notify();
    }
    public synchronized void des(){
      while(isSync){
        try {
          this.wait();
          Thread.sleep(1000L);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("下載結(jié)束....");
      isSync = true;
      this.notify();
    }
  }
}

上述內(nèi)容就是JAVA中怎么使用synchronized、wait、notify實(shí)現(xiàn)同步通信,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文標(biāo)題:JAVA中怎么使用synchronized、wait、notify實(shí)現(xiàn)同步通信
網(wǎng)址分享:http://muchs.cn/article36/phoipg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、微信小程序靜態(tài)網(wǎng)站、搜索引擎優(yōu)化網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)