KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > performance > generate > load > MonitoredWorkQueue


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.performance.generate.load;
5
6 import com.tc.util.Assert;
7
8 import java.util.LinkedList JavaDoc;
9 import java.util.List JavaDoc;
10
11 /**
12  * @ThreadSafe
13  * @LivenessTuned
14  */

15 final class MonitoredWorkQueue {
16
17   private static final int TIMEOUT = 5000;
18   private final Object JavaDoc[] queue;
19   private final long[] intervalStartTime;
20   private final List JavaDoc points;
21   private int count, first, last;
22   private long intervalMagnitude;
23   private final Object JavaDoc intervalLock = new Object JavaDoc();
24   private volatile boolean overflow;
25   private final int quarterCapacity;
26   private final boolean keepMetronome;
27   
28   // allows server to catch up when blocking for 2min at saturation point
29
private int waitCount;
30
31   MonitoredWorkQueue(int size, boolean keepMetronome) {
32     Assert.assertTrue(size > 0);
33     this.queue = new Object JavaDoc[size];
34     this.intervalStartTime = new long[size];
35     this.points = new LinkedList JavaDoc();
36     this.keepMetronome = keepMetronome;
37     this.quarterCapacity = size / 4;
38   }
39
40   synchronized void put(Object JavaDoc obj) {
41     if (count == quarterCapacity) {
42       System.err.println("WORK QUEUE HAS REACHED 25% CAPACITY - NOW WOULD BE A GOOD TIME FOR A THREAD DUMP");
43       if (waitCount++ < 2) {
44         System.err.println("PAUSE (2 seconds)");
45         try {
46           Thread.sleep(1000 * 2);
47         } catch (InterruptedException JavaDoc e) {
48           throw new RuntimeException JavaDoc(e);
49         }
50       }
51     }
52     if (count == queue.length) {
53       overflow = true;
54       while (true)
55         try {
56           wait();
57         } catch (InterruptedException JavaDoc e) {
58           // ignore
59
}
60     }
61
62     boolean startTimer = false;
63     if (obj instanceof Metronome) {
64       if (!keepMetronome) obj = ((Metronome) obj).object;
65       startTimer = true;
66     }
67
68     queue[last] = obj;
69     last = (last + 1) % queue.length;
70     count++;
71     if (startTimer) intervalStartTime[last] = System.nanoTime();
72     notifyAll();
73   }
74
75   synchronized Object JavaDoc get() throws InterruptedException JavaDoc, WorkQueueOverflowException {
76     if (overflow) throw new WorkQueueOverflowException();
77     long waitTimeout;
78     // timeout is more efficient than a poison pill
79
while (count == 0) {
80       waitTimeout = System.currentTimeMillis();
81       wait(TIMEOUT);
82       if ((System.currentTimeMillis() - waitTimeout) >= TIMEOUT) return null; // detect completion
83
}
84
85     Object JavaDoc obj = queue[first];
86     queue[first] = null;
87     count--;
88     first = (first + 1) % queue.length;
89
90     // manage measurements
91
if (intervalStartTime[first] != 0L) {
92       long waitTime = System.nanoTime() - intervalStartTime[first];
93       synchronized (points) {
94         points.add(new Measurement(intervalMagnitude, waitTime));
95       }
96       synchronized (intervalLock) {
97         if (keepMetronome) ((Metronome) obj).load = intervalMagnitude;
98         intervalMagnitude = 0;
99         intervalStartTime[first] = 0L;
100       }
101     }
102     synchronized (intervalLock) {
103       intervalMagnitude++;
104     }
105
106     return obj;
107   }
108
109   Measurement[] getWaitTimes() {
110     synchronized (points) {
111       return (Measurement[]) points.toArray(new Measurement[0]);
112     }
113   }
114 }
115
Popular Tags