⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 producer.java

📁 Java5.0 Tiger 程序员高手秘笈一书的源代码
💻 JAVA
字号:
package com.oreilly.tiger.ch10;

import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

public class Producer extends Thread {

  private BlockingQueue q;
  private PrintStream out;

  public Producer(BlockingQueue q, PrintStream out) {
    setName("Producer");
    this.q = q;
    this.out = out;
  }

  public void run() {
    try {
      while (true) {
        q.put(produce());
      }
    } catch (InterruptedException e) {
      out.printf("%s interrupted: %s", getName(), e.getMessage());
    }
  }

  private String produce() {
    while (true) {
      double r = Math.random();

      // Only goes forward 1/10 of the time
      if ((r*100) < 10) {
        String s = String.format("Inserted at %tc", new Date());
        return s;
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -