[Java concurrent programming] 3. DelayQueue Application Scenario, multi-examinee test, javadelayqueue

Source: Internet
Author: User

[Java concurrent programming] 3. DelayQueue Application Scenario, multi-examinee test, javadelayqueue

This scenario comes from: http://www.cnblogs.com/sunzhenchao/p/3515085.html.

Simulate the day of a test. The test time is 120 minutes. You can submit the exam only 30 minutes later. When the time is up, or the students finish the exam.

Note the following points in this scenario:

Abstract two classes: Student Class and teacher Class. Use DelayQueue to store the Student class ). Each candidate has his/her own name and time to finish the exam.

The Teacher thread monitors the DelayQueue and collects the examination papers of students whose test is completed less than 120 minutes. When the test time is 120 minutes, the teacher thread announces that the test is complete and forces the exam to be handed in by another student in DelayQueue.

Package zhongqiu. common. base. thread; import java. util. iterator; import java. util. random; import java. util. concurrent. delayQueue; import java. util. concurrent. delayed; import java. util. concurrent. timeUnit; public class detail {public static void main (String [] args) throws InterruptedException {// TODO Auto-generated method stubint studentNumber = 20; DelayQueue <Student> students = new DelayQueue <Student> (); Random random = new Random (); for (int I = 0; I <studentNumber; I ++) {students. put (new Student ("student" + (I + 1), 30 + random. nextInt (120);} students. put (new Student ("student", 120); Thread teacherThread = new Thread (new Teacher (students); teacherThread. start () ;}} class Student implements Runnable, Delayed {private String name; public long workTime; private long submitTime; private boolea N isForce = false; public Student () {} public Student (String name, long workTime) {this. name = name; this. workTime = workTime; this. submitTime = TimeUnit. NANOSECONDS. convert (workTime, TimeUnit. NANOSECONDS) + System. nanoTime (); // nanosecond level} @ Overridepublic int compareTo (Delayed o) {// TODO Auto-generated method stubif (o = null |! (O instanceof Student) return 1; if (o = this) return 0; Student s = (Student) o; if (this. workTime> s. workTime) {return 1;} else if (this. workTime = s. workTime) {return 0;} else {return-1 ;}@ Overridepublic long getDelay (TimeUnit unit) {// TODO Auto-generated method stubreturn unit. convert (submitTime-System. nanoTime (), TimeUnit. NANOSECONDS) ;}@ Overridepublic void run () {// TODO Auto-generated meth Od stubif (isForce) {System. out. println (name + ", actual use of 120 minutes");} else {System. out. println (name + "," + "" + workTime + "min") ;}} public boolean isForce () {return isForce;} public void setForce (boolean isForce) {this. isForce = isForce ;}} class Teacher implements Runnable {private int counter = 20; private DelayQueue <Student> students; public Teacher (DelayQueue <Student> students) {this. students = Students;} @ Overridepublic void run () {// TODO Auto-generated method stubtry {System. out. println ("test start"); while (counter> 0) {Student student = students. poll (); if (student. workTime <120) {student. run (); if (counter> 0) {counter -- ;}} else {System. out. println ("the exam time is up, submit all exams! "); Student tmpStudent; for (Iterator <Student> iterator2 = students. iterator (); iterator2.hasNext ();) {tmpStudent = iterator2.next (); tmpStudent. setForce (true); tmpStudent. run (); if (counter> 0) {counter -- ;}}} catch (Exception e) {// TODO: handle has tione. printStackTrace ();}}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.