corba/src/share/classes/com/sun/corba/se/impl/orbutil/threadpool/WorkQueueImpl.java
author coffeys
Wed, 27 Jun 2012 21:09:29 +0100
changeset 13171 1ac5e9a54a6e
parent 5555 b2b5ed3f0d0d
permissions -rw-r--r--
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8 Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.orbutil.threadpool;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.util.LinkedList;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import com.sun.corba.se.spi.orbutil.threadpool.ThreadPool;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import com.sun.corba.se.spi.orbutil.threadpool.Work;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import com.sun.corba.se.impl.orbutil.ORBConstants;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import com.sun.corba.se.impl.orbutil.threadpool.ThreadPoolImpl;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import com.sun.corba.se.spi.monitoring.MonitoringConstants;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import com.sun.corba.se.spi.monitoring.MonitoringFactories;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import com.sun.corba.se.spi.monitoring.MonitoredObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import com.sun.corba.se.spi.monitoring.LongMonitoredAttributeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
public class WorkQueueImpl implements WorkQueue
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
    private ThreadPool workerThreadPool;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
    private LinkedList theWorkQueue = new LinkedList();
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
    private long workItemsAdded = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
    // Initialized to 1 to avoid divide by zero in averageTimeInQueue()
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
    private long workItemsDequeued = 1;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
    private long totalTimeInQueue = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
    // Name of the work queue
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
    private String name;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
    // MonitoredObject for work queue
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
    private MonitoredObject workqueueMonitoredObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    public WorkQueueImpl() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
        name=ORBConstants.WORKQUEUE_DEFAULT_NAME;
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
        initializeMonitoring();
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
    public WorkQueueImpl(ThreadPool workerThreadPool) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        this(workerThreadPool, ORBConstants.WORKQUEUE_DEFAULT_NAME);
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    public WorkQueueImpl(ThreadPool workerThreadPool, String name) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
        this.workerThreadPool = workerThreadPool;
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
        this.name = name;
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
        initializeMonitoring();
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
    // Setup monitoring for this workqueue
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
    private void initializeMonitoring() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
        workqueueMonitoredObject = MonitoringFactories.
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
                            getMonitoredObjectFactory().
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
                            createMonitoredObject(name,
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
                            MonitoringConstants.WORKQUEUE_MONITORING_DESCRIPTION);
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
        LongMonitoredAttributeBase b1 = new
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
            LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_TOTAL_WORK_ITEMS_ADDED,
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
                    MonitoringConstants.WORKQUEUE_TOTAL_WORK_ITEMS_ADDED_DESCRIPTION) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
                public Object getValue() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
                    return new Long(WorkQueueImpl.this.totalWorkItemsAdded());
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
            };
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
        workqueueMonitoredObject.addAttribute(b1);
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
        LongMonitoredAttributeBase b2 = new
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
            LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_WORK_ITEMS_IN_QUEUE,
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
                    MonitoringConstants.WORKQUEUE_WORK_ITEMS_IN_QUEUE_DESCRIPTION) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
                public Object getValue() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
                    return new Long(WorkQueueImpl.this.workItemsInQueue());
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
            };
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
        workqueueMonitoredObject.addAttribute(b2);
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        LongMonitoredAttributeBase b3 = new
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
            LongMonitoredAttributeBase(MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE,
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
                    MonitoringConstants.WORKQUEUE_AVERAGE_TIME_IN_QUEUE_DESCRIPTION) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
                public Object getValue() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
                    return new Long(WorkQueueImpl.this.averageTimeInQueue());
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
            };
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        workqueueMonitoredObject.addAttribute(b3);
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
    // Package private method to get the monitored object for this
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
    // class
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
    MonitoredObject getMonitoredObject() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
        return workqueueMonitoredObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   114
    public synchronized void addWork(Work work) {
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
            workItemsAdded++;
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
            work.setEnqueueTime(System.currentTimeMillis());
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            theWorkQueue.addLast(work);
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
            ((ThreadPoolImpl)workerThreadPool).notifyForAvailableWork(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   121
    synchronized Work requestWork(long waitTime) throws TimeoutException, InterruptedException
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
        Work workItem;
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   124
        ((ThreadPoolImpl)workerThreadPool).incrementNumberOfAvailableThreads();
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   125
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
            if (theWorkQueue.size() != 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
                workItem = (Work)theWorkQueue.removeFirst();
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
                totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
                workItemsDequeued++;
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   130
                ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
                return workItem;
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
                long remainingWaitTime = waitTime;
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
                long finishTime = System.currentTimeMillis() + waitTime;
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
                do {
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
                    this.wait(remainingWaitTime);
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
                    if (theWorkQueue.size() != 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
                        workItem = (Work)theWorkQueue.removeFirst();
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
                        totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
                        workItemsDequeued++;
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   147
                        ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
                        return workItem;
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
                    remainingWaitTime = finishTime - System.currentTimeMillis();
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
                } while (remainingWaitTime > 0);
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   155
                ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
                throw new TimeoutException();
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
            } catch (InterruptedException ie) {
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 5555
diff changeset
   159
                ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
                throw ie;
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
    public void setThreadPool(ThreadPool workerThreadPool) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
            this.workerThreadPool = workerThreadPool;
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
    public ThreadPool getThreadPool() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
            return workerThreadPool;
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
     * Returns the total number of Work items added to the Queue.
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
     * This method is unsynchronized and only gives a snapshot of the
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
     * state when it is called
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
    public long totalWorkItemsAdded() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
        return workItemsAdded;
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
     * Returns the total number of Work items in the Queue to be processed
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
     * This method is unsynchronized and only gives a snapshot of the
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
     * state when it is called
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
    public int workItemsInQueue() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
        return theWorkQueue.size();
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
    public synchronized long averageTimeInQueue() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        return (totalTimeInQueue/workItemsDequeued);
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
    public String getName() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
        return name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
// End of file.