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

📄 msgsender.java

📁 一步 教你学会java 完成一个完整的SWT系统 是本人学习java时按教程一步一步写下来的咯
💻 JAVA
字号:
/** *  Concurrency utilities (JSR-166) example **/package queue;import java.net.*;import java.io.*;import java.util.concurrent.*;/** *  Send messages to a Logger via a BlockingQueue **/public class MsgSender implements Runnable {  private BlockingQueue<String> messageQueue;  private int id;  private int count;  private long pause;  /**   *  Constructor   *   * @param messageQueue The quese to send messages to   * @param id The ID number of this sender   * @param count The number of messages to send   * @param pause The pause between sending each message   **/  public MsgSender(BlockingQueue<String> messageQueue, int id,      int count, long pause) {    this.messageQueue = messageQueue;    this.id = id;    this.count = count;    this.pause = pause;  }  /**   *  Run method to send the messages   **/  public void run() {    try {      for (int i = 0; i < count; i++) {        messageQueue.put("ID " + id + ": log message number " + i);        Thread.sleep(pause);      }    } catch (InterruptedException ie) {      //  Ignore    }  }  /**   *  Main entry point for running test scenario   *   * @param args The command line arguments   **/  public static void main(String[] args) {    /*  For the test we will need a BlockingQueue to be used by both threads     *  Initially use an ArrayBlockingQueue which is the simplest concrete      *  implementation of the BlockingQueue interface.  The constructor takes     *  the size of the queue as a parameter     */    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);    /*  Use the utility method from the Executors class to get an     *  ExcutorService reference that will allow us to execute a single     *  thread     */    ExecutorService loggerExecutor = Executors.newSingleThreadExecutor();    loggerExecutor.execute(new Logger(queue));    /*  Again use the utility Executors class to get a new ExecutorService      *  for a second new thread and pass a MsgSender instance to it to run     */    ExecutorService senderExecutor = Executors.newSingleThreadExecutor();    senderExecutor.execute(new MsgSender(queue, 1, 10, 500));  }}

⌨️ 快捷键说明

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