Java線程學習 DelayQueue的應用__多線程

來源:互聯網
上載者:User

DelayQueue

是一個無界的BlockingQueue,用於放置實現了Delayed介面的對象,其中的對象只能在其到期時才能從隊列中取走。這種隊列是有序的,即隊頭對象的延遲到期時間最長。注意:不能將null元素放置到這種隊列中。

 

Delayed

一種混合風格的介面,用來標記那些應該在給定延遲時間之後執行的對象。

此介面的實現必須定義一個 compareTo 方法,該方法提供與此介面的 getDelay 方法一致的排序。

 

下面的代碼類比一個考試的日子,考試時間為120分鐘,30分鐘後才可交卷,當時間到了,或學生都交完卷了者考試結束。線程的關閉參考Java編程思想中例子,將exec傳給Student的一個內部類,通過他來關閉。

Java代碼   package com.woxiaoe.study.thread;      import java.util.Random;   import java.util.concurrent.DelayQueue;   import java.util.concurrent.Delayed;   import java.util.concurrent.ExecutorService;   import java.util.concurrent.Executors;   import java.util.concurrent.TimeUnit;      /**   * 類比考試,時間為120分鐘,學生可以再30分鐘後交卷,   * 當學生都交完了 或 時間到者考試結束   * @author 小e   *   * 2010-4-30 下午11:14:25   */   class Student implements Runnable,Delayed{       private String name;       private long submitTime;//交卷時間       private long workTime;//考試時間       public Student() {           // TODO Auto-generated constructor stub       }       public Student(String name, long submitTime) {           super();           this.name = name;           workTime = submitTime;           //都轉為轉為ns           this.submitTime = TimeUnit.NANOSECONDS.convert(submitTime, TimeUnit.MILLISECONDS) + System.nanoTime();       }          @Override       public void run() {           System.out.println(name + " 交卷,用時" + workTime/100 + "分鐘");       }          @Override       public long getDelay(TimeUnit unit) {           return unit.convert(submitTime - System.nanoTime(), unit.NANOSECONDS);       }          @Override       public int compareTo(Delayed o) {           Student that = (Student) o;           return submitTime > that.submitTime?1:(submitTime < that.submitTime ? -1 : 0);       }       public static class EndExam extends Student{           private ExecutorService exec;           public EndExam(int submitTime,ExecutorService exec) {               super(null,submitTime);               this.exec = exec;           }           @Override           public void run() {               exec.shutdownNow();           }       }          }   class Teacher implements Runnable{       private DelayQueue<Student> students;       private ExecutorService exec;              public Teacher(DelayQueue<Student> students,ExecutorService exec) {           super();           this.students = students;           this.exec = exec;       }             @Override       public void run() {           try {               System.out.println("考試開始……");               while (!Thread.interrupted()) {                   students.take().run();               }               System.out.println("考試結束……");           } catch (InterruptedException e) {               e.printStackTrace();           }          }          }   public class Exam {       static final int STUDENT_SIZE = 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.