summaryrefslogtreecommitdiff
path: root/java/tests/ASX/ThreadPoolTest.java
blob: 6367452131a6e680a0ac11edbdd3ab4caedcc1a4 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// ============================================================================
//
// = PACKAGE
//    tests.ASX
// 
// = FILENAME
//    ThreadPoolTest.java
//
// = AUTHOR
//    Prashant Jain
// 
// ============================================================================
package tests.ASX;

import java.io.*;
import ACE.OS.*;
import ACE.ASX.*;
import ACE.Reactor.*;

public class ThreadPoolTest extends Task
{
  int nThreads_;
  int nIterations_;
  
  public static int MAX_MB_SIZE = 1024;

  public ThreadPoolTest (int nThreads, int nIterations)
    {
      this.nIterations_ = nIterations;
      this.nThreads_ = nThreads;
      if (this.activate (0, nThreads, true) == -1)
	ACE.ERROR ("activate failed");
    }

  public int handleTimeout (TimeValue tv, Object obj)
  {
    return 0;
  }

  public int open (Object obj)
    {
      return 0;
    }

  public int close (long flags)
    {
      return 0;
    }

  public int put (MessageBlock mb, TimeValue tv)
    {
      try
	{
	  return this.putq (mb, tv);
	}
      catch (InterruptedException e)
	{
	}
      return 0;
    }
  
  public int svc ()
    {
      int result = 0;
      int count = 1;

      // Keep looping, reading a message out of the queue, until we get a
      // message with a length == 0, which signals us to quit.
      try
	{
	  for (;; count++)
	    {       
	      MessageBlock mb = this.getq (new TimeValue ());
	      if (mb == null)
		{
		  ACE.ERROR (Thread.currentThread ().toString () + " in iteration " + count + ", got result -1, exiting");
		  break;
		}
	      int length = mb.length ();
	      
	      if (length > 0)
		ACE.DEBUG (Thread.currentThread ().toString () +
			   " in iteration " + count + ", length = " + 
			   length + ", text = \"" + mb.base () + "\"");
	      
	      if (length == 0)
		{
		  ACE.DEBUG (Thread.currentThread ().toString () +
			     " in iteration " + count + 
			     ", got NULL message, exiting");
		  break;
		}
	      Thread.yield ();
	    }
	}
      catch (InterruptedException e)
	{
	}
      return 0;
    }

  public static void produce (ThreadPoolTest threadPool, int nIterations)
    {
      int count = 0;
      for (int n = 0;;)
	{
	  // Allocate a new message.
	  MessageBlock mb = new MessageBlock (new Integer (count).toString ());
	  
	  if (count == nIterations)
	    n = 1; // Indicate that we need to shut down.
	  else
	    count++;

	  if (count == 0 || (count % 20 == 0))
	    {
	      try
		{
		  Thread.sleep (1);
		}
	      catch (InterruptedException e)
		{
		}
	    }
	  if (n != 1)
	    {
	      ACE.DEBUG ("Producing...");
	      // Pass the message to the Thread_Pool.
	      if (threadPool.put (mb, new TimeValue ()) == -1)
		ACE.ERROR ("put");
	    }
	  else
	    {
	      // Send a shutdown message to the waiting threads and exit.
	      ACE.DEBUG ("start loop, dump of task");

	      for (int i = threadPool.thrCount (); i > 0; i--)
		{
		  ACE.DEBUG (Thread.currentThread ().toString () + 
			     "EOF, enqueueing NULL block for thread " + i);

		  // Enqueue a NULL message to flag each consumer to
		  // shutdown.
		  if (threadPool.put (new MessageBlock (0), new TimeValue ()) == -1)
		    ACE.ERROR ("put");
		}

	      break;
	    }
	}
    }

  public static void main (String args[])
    {
      int nThreads = 1;
      int nIterations = 100;
      try
	{
	  if (args.length == 2)
	    {
	      nThreads = Integer.parseInt (args[0]);
	      nIterations = Integer.parseInt (args[1]);
	    }
	  else if (args.length == 1)
	    {
	      nThreads = Integer.parseInt (args[0]);
	    }
	}
      catch (NumberFormatException e)
	{
	  ACE.ERROR ("Illegal argument.");
	}
      ACE.DEBUG ("Threads = " + nThreads + " Iterations = " + nIterations);

      // Create the worker tasks.
      ThreadPoolTest threadPool = new ThreadPoolTest (nThreads,
						      nIterations);

      // Create work for the worker tasks to process in their own threads.
      produce (threadPool, nIterations);
      ACE.DEBUG ("exiting...");
    }
}