📄 producer.java
字号:
// This class encapsulates a Producer. Each new instance of this class // creates a different thread which tries to nq into the queue // Currently queues random values generated by the Random class // If timeout expires, the Producer instance returns //package NexusII.util ; import java.util.Random ; public class Producer extends Thread {// If no time out is desired, timeout value is set to one. so the run method// knows which nq to call public Producer(MT_Bounded_Queue queue) { this.queue_ = queue ; this.iterations_ = new Integer(DEFAULT_ITERATIONS); this.time_out_ = -1 ; }// Include the name of the thread as a parameter public Producer(MT_Bounded_Queue queue, String name) { super(name); this.queue_ = queue ; this.iterations_ = new Integer(DEFAULT_ITERATIONS); this.time_out_ = -1 ; }// If the number of iterations are also included -- public Producer(MT_Bounded_Queue queue, String name, Integer iterations) { super(name); this.queue_ = queue ; iterations_ = iterations ; this.time_out_ = -1 ; }// Finally, if the timeout period is also included public Producer(MT_Bounded_Queue queue, String name, Integer iterations, long msec_timeout) { super(name); this.queue_ = queue ; iterations_ = iterations ; this.time_out_ = msec_timeout ; } // The hook method called by start()public void run() { // Initialize the random number generator Random rand = new Random(); for(int i=0;i<iterations_.intValue();i++) { int err = 0 ; // Get the next random value for insertion into queue Integer new_item = new Integer(rand.nextInt()) ; // Doesnt make sense to have a negative timeout -- default if(time_out_ < 0) queue_.nq(new_item); else err = queue_.nq(new_item,time_out_); // If timedout stop this thread if(err == -1) { System.out.println(getName() + ": Timed Out \n"); return ; } System.out.println(getName() + ": enqueued " + new_item.intValue()); } }private static final int DEFAULT_ITERATIONS = 1 ; protected MT_Bounded_Queue queue_ ; private Integer iterations_ ; private long time_out_ ; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -