summaryrefslogtreecommitdiff
path: root/java/tests/Concurrency/Condition/QueueTest.java
blob: e733c3cc21eb0da7757a615afa1ff02fa0ee3b87 (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
//File: QueueTest.java
//Seth Widoff, 8/8/96
//This class is a test method for the Producer and Consumer classes.
//The main method takes as arguments the number of producers, the
//number of consumers and the number of elements in the queue. It then
//spawn the specified threads and starts them.

package tests.Concurrency.Condition;

import ACE.ASX.TimeValue;

public class QueueTest
{
  public static void main(String[] args)
  {
    if (args.length < 5)
      {
	System.out.println("Usage: java QueueTest <# producers> <# consumers> <# elements> <#iterations> <#timeout secs> <#timeout nano secs>");
	System.exit(1);
      }
    
    int num_producers = Integer.parseInt(args[0]),
      num_consumers = Integer.parseInt(args[1]),
      num_elements = Integer.parseInt(args[2]),
      num_iterations = Integer.parseInt(args[3]),
      num_timeout_secs = Integer.parseInt(args[4]),
      num_timeout_nano_secs = Integer.parseInt(args[5]);
    
    if (num_elements < 1 
	|| num_consumers < 1 
	|| num_producers < 1)
      {
	System.out.println("All the parameters must be larger than zero.");
	System.exit(1);
      }
    
    SimpleMessageQueue queue = new SimpleMessageQueue(num_elements);
    Consumer[] consumers = new Consumer[num_consumers];
    Producer[] producers = new Producer[num_producers];
    JoinableThreadGroup thread_group = new JoinableThreadGroup("Producer Consumer");
    
    for (int i = 0; i < num_producers; i++)
      {
	producers[i] = new Producer("Number " + (i + 1), queue, num_iterations, new TimeValue (num_timeout_secs, num_timeout_nano_secs));
	new Thread(thread_group, producers[i]).start();
      }

    for (int i = 0; i < num_consumers; i++)
      {
	consumers[i] = new Consumer("Number " + (i + 1), queue, num_iterations, new TimeValue (num_timeout_secs, num_timeout_nano_secs));
	new Thread(thread_group, consumers[i]).start();
      }

    try
      {
	thread_group.join();
      }
    catch(InterruptedException excp)
      {
	System.out.println("QueueTest::main");
	System.out.println(excp);
      }
  }
}