corba/src/share/classes/com/sun/corba/se/impl/orbutil/threadpool/WorkQueueImpl.java
changeset 13171 1ac5e9a54a6e
parent 5555 b2b5ed3f0d0d
equal deleted inserted replaced
13082:9b19b2302c28 13171:1ac5e9a54a6e
     1 /*
     1 /*
     2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   109     // class
   109     // class
   110     MonitoredObject getMonitoredObject() {
   110     MonitoredObject getMonitoredObject() {
   111         return workqueueMonitoredObject;
   111         return workqueueMonitoredObject;
   112     }
   112     }
   113 
   113 
   114     public void addWork(Work work) {
   114     public synchronized void addWork(Work work) {
   115         synchronized (this) {
       
   116             workItemsAdded++;
   115             workItemsAdded++;
   117             work.setEnqueueTime(System.currentTimeMillis());
   116             work.setEnqueueTime(System.currentTimeMillis());
   118             theWorkQueue.addLast(work);
   117             theWorkQueue.addLast(work);
   119             ((ThreadPoolImpl)workerThreadPool).notifyForAvailableWork(this);
   118             ((ThreadPoolImpl)workerThreadPool).notifyForAvailableWork(this);
   120         }
       
   121     }
   119     }
   122 
   120 
   123     Work requestWork(long waitTime)
   121     synchronized Work requestWork(long waitTime) throws TimeoutException, InterruptedException
   124         throws TimeoutException, InterruptedException
       
   125     {
   122     {
   126         Work workItem;
   123         Work workItem;
   127         synchronized (this) {
   124         ((ThreadPoolImpl)workerThreadPool).incrementNumberOfAvailableThreads();
       
   125 
   128             if (theWorkQueue.size() != 0) {
   126             if (theWorkQueue.size() != 0) {
   129                 workItem = (Work)theWorkQueue.removeFirst();
   127                 workItem = (Work)theWorkQueue.removeFirst();
   130                 totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
   128                 totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
   131                 workItemsDequeued++;
   129                 workItemsDequeued++;
       
   130                 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
   132                 return workItem;
   131                 return workItem;
   133             }
   132             }
   134 
   133 
   135             try {
   134             try {
   136 
   135 
   143 
   142 
   144                     if (theWorkQueue.size() != 0) {
   143                     if (theWorkQueue.size() != 0) {
   145                         workItem = (Work)theWorkQueue.removeFirst();
   144                         workItem = (Work)theWorkQueue.removeFirst();
   146                         totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
   145                         totalTimeInQueue += System.currentTimeMillis() - workItem.getEnqueueTime();
   147                         workItemsDequeued++;
   146                         workItemsDequeued++;
       
   147                         ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
   148                         return workItem;
   148                         return workItem;
   149                     }
   149                     }
   150 
   150 
   151                     remainingWaitTime = finishTime - System.currentTimeMillis();
   151                     remainingWaitTime = finishTime - System.currentTimeMillis();
   152 
   152 
   153                 } while (remainingWaitTime > 0);
   153                 } while (remainingWaitTime > 0);
   154 
   154 
       
   155                 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
   155                 throw new TimeoutException();
   156                 throw new TimeoutException();
   156 
   157 
   157             } catch (InterruptedException ie) {
   158             } catch (InterruptedException ie) {
       
   159                 ((ThreadPoolImpl)workerThreadPool).decrementNumberOfAvailableThreads();
   158                 throw ie;
   160                 throw ie;
   159             }
   161             }
   160         }
       
   161     }
   162     }
   162 
   163 
   163     public void setThreadPool(ThreadPool workerThreadPool) {
   164     public void setThreadPool(ThreadPool workerThreadPool) {
   164             this.workerThreadPool = workerThreadPool;
   165             this.workerThreadPool = workerThreadPool;
   165     }
   166     }