summaryrefslogtreecommitdiff
path: root/java/apps/NexusII/src/Producer.java
blob: 4153f7d79df67e4fe5fec1ae1b1ebd0f24776557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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_ ; 
}