1 /*
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
94 }
95 };
96 workqueueMonitoredObject.addAttribute(b2);
97 LongMonitoredAttributeBase b3 = new
98 LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE,
99 MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE_DESCRIPTION) {
100 public Object getValue() {
101 return new Long(WorkQueueImpl.this.averageTimeInQueue());
102 }
103 };
104 workqueueMonitoredObject.addAttribute(b3);
105 }
106
107
108 // Package private method to get the monitored object for this
109 // class
110 MonitoredObject getMonitoredObject() {
111 return workqueueMonitoredObject;
112 }
113
114 public void addWork(Work work) {
115 synchronized (this) {
116 workItemsAdded++;
117 work.setEnqueueTime(System.currentTimeMillis());
118 theWorkQueue.addLast(work);
119 ((ThreadPoolImpl)workerThreadPool).notifyForAvailableWork(this);
120 }
121 }
122
123 Work requestWork(long waitTime)
124 throws TimeoutException, InterruptedException
125 {
126 Work workItem;
127 synchronized (this) {
128 if (theWorkQueue.size() != 0) {
129 workItem = (Work)theWorkQueue.removeFirst();
130 totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
131 workItemsDequeued++;
132 return workItem;
133 }
134
135 try {
136
137 long remainingWaitTime = waitTime;
138 long finishTime = System.currentTimeMillis() + waitTime;
139
140 do {
141
142 this.wait(remainingWaitTime);
143
144 if (theWorkQueue.size() != 0) {
145 workItem = (Work)theWorkQueue.removeFirst();
146 totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
147 workItemsDequeued++;
148 return workItem;
149 }
150
151 remainingWaitTime = finishTime - System.currentTimeMillis();
152
153 } while (remainingWaitTime > 0);
154
155 throw new TimeoutException();
156
157 } catch (InterruptedException ie) {
158 throw ie;
159 }
160 }
161 }
162
163 public void setThreadPool(ThreadPool workerThreadPool) {
164 this.workerThreadPool = workerThreadPool;
165 }
166
167 public ThreadPool getThreadPool() {
168 return workerThreadPool;
169 }
170
171 /**
172 * Returns the total number of Work items added to the Queue.
173 * This method is unsynchronized and only gives a snapshot of the
174 * state when it is called
175 */
176 public long totalWorkItemsAdded() {
177 return workItemsAdded;
178 }
179
180 /**
181 * Returns the total number of Work items in the Queue to be processed
|
1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
94 }
95 };
96 workqueueMonitoredObject.addAttribute(b2);
97 LongMonitoredAttributeBase b3 = new
98 LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE,
99 MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE_DESCRIPTION) {
100 public Object getValue() {
101 return new Long(WorkQueueImpl.this.averageTimeInQueue());
102 }
103 };
104 workqueueMonitoredObject.addAttribute(b3);
105 }
106
107
108 // Package private method to get the monitored object for this
109 // class
110 MonitoredObject getMonitoredObject() {
111 return workqueueMonitoredObject;
112 }
113
114 public synchronized void addWork(Work work) {
115 workItemsAdded++;
116 work.setEnqueueTime(System.currentTimeMillis());
117 theWorkQueue.addLast(work);
118 ((ThreadPoolImpl)workerThreadPool).notifyForAvailableWork(this);
119 }
120
121 synchronized Work requestWork(long waitTime) throws TimeoutException, InterruptedException
122 {
123 Work workItem;
124 ((ThreadPoolImpl)workerThreadPool).incrementNumberOfAvailableThreads();
125
126 if (theWorkQueue.size() != 0) {
127 workItem = (Work)theWorkQueue.removeFirst();
128 totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
129 workItemsDequeued++;
130 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
131 return workItem;
132 }
133
134 try {
135
136 long remainingWaitTime = waitTime;
137 long finishTime = System.currentTimeMillis() + waitTime;
138
139 do {
140
141 this.wait(remainingWaitTime);
142
143 if (theWorkQueue.size() != 0) {
144 workItem = (Work)theWorkQueue.removeFirst();
145 totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
146 workItemsDequeued++;
147 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
148 return workItem;
149 }
150
151 remainingWaitTime = finishTime - System.currentTimeMillis();
152
153 } while (remainingWaitTime > 0);
154
155 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
156 throw new TimeoutException();
157
158 } catch (InterruptedException ie) {
159 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
160 throw ie;
161 }
162 }
163
164 public void setThreadPool(ThreadPool workerThreadPool) {
165 this.workerThreadPool = workerThreadPool;
166 }
167
168 public ThreadPool getThreadPool() {
169 return workerThreadPool;
170 }
171
172 /**
173 * Returns the total number of Work items added to the Queue.
174 * This method is unsynchronized and only gives a snapshot of the
175 * state when it is called
176 */
177 public long totalWorkItemsAdded() {
178 return workItemsAdded;
179 }
180
181 /**
182 * Returns the total number of Work items in the Queue to be processed
|