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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

生产者、消费者问题之闹钟

發(fā)布時(shí)間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 生产者、消费者问题之闹钟 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

需求:寫(xiě)一個(gè)鬧鐘程序,鬧鐘每隔2秒響一次,每次響5聲,鬧鐘響鈴后1秒,人將鬧鐘關(guān)閉.
public class People implements Runnable {
  private String name;
  private Clock c;

  public People(String name, Clock c) {
  this.name = name;
    this.c = c;
  }

  public void run() {
    weakup();
  }

  public void weakup() {
    //睡覺(jué)的人時(shí)刻等待鬧鈴響
    while(true) {
      synchronized(c) {
        if(c.isRing()) {
          c.setRing(false);
          System.out.println("就起來(lái)了就起來(lái)");
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e1) {
            e1.printStackTrace();
          }
          c.notify();
          try {
            c.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }else {
          c.setRing(true);
          c.notify();
          try {
            c.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Clock1 getC() {
    return c;
  }

  public void setC(Clock1 c) {
    this.c = c;
  }
}

?

?

public class Clock implements Runnable {
  private boolean isRing; //判斷鬧鈴是否響過(guò)

  public void run() {
    ring();
  }

  public void ring() {
    //鬧鈴響5次,每次響3聲
    for(int i = 0; i < 5; i++) {
      synchronized(this) {
      if(!isRing) {
        isRing = true;
        for(int j = 0; j < 3; j++) {
          System.out.println("叮叮叮...");
        }
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e1) {
          e1.printStackTrace();
        }
        this.notify();
        try {
          this.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
       }else {
        try {
          isRing = false;
          this.notify();
          this.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }

  public boolean isRing() {
    return isRing;
  }

  public void setRing(boolean isRing) {
    this.isRing = isRing;
  }
}

?

public class TestClockPeople {
  public static void main(String [] args) {
    Clock c = new Clock1();
    People p = new People("大姚", c);

    Thread ct = new Thread(c);
    Thread pt = new Thread(p);

    pt.setDaemon(true); //將人設(shè)置為守護(hù)線程
    ct.start();
    pt.start();
  }
}

轉(zhuǎn)載于:https://www.cnblogs.com/helloworldlx/p/8550356.html

總結(jié)

以上是生活随笔為你收集整理的生产者、消费者问题之闹钟的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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