You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by xu...@apache.org on 2010/09/09 12:12:25 UTC

svn commit: r995369 - in /geronimo/server/trunk/framework/modules/geronimo-core/src: main/java/org/apache/geronimo/pool/ThreadPool.java test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java

Author: xuhaihong
Date: Thu Sep  9 10:12:24 2010
New Revision: 995369

URL: http://svn.apache.org/viewvc?rev=995369&view=rev
Log:
a.GERONIMO-5491 Add a test case provided by Han Hong Fang
b.Format the codes of ThreadPool

Added:
    geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java   (with props)
Modified:
    geronimo/server/trunk/framework/modules/geronimo-core/src/main/java/org/apache/geronimo/pool/ThreadPool.java

Modified: geronimo/server/trunk/framework/modules/geronimo-core/src/main/java/org/apache/geronimo/pool/ThreadPool.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-core/src/main/java/org/apache/geronimo/pool/ThreadPool.java?rev=995369&r1=995368&r2=995369&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-core/src/main/java/org/apache/geronimo/pool/ThreadPool.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-core/src/main/java/org/apache/geronimo/pool/ThreadPool.java Thu Sep  9 10:12:24 2010
@@ -54,22 +54,23 @@ public class ThreadPool implements Geron
     private ClassLoader classLoader;
     private ObjectName objectName;
     private boolean waitWhenBlocked;
-    
+
     // Statistics-related fields follow
     private boolean statsActive = true;
     private PoolStatsImpl stats = new PoolStatsImpl();
-    private Map clients = new HashMap();
+
+    private Map<String, Integer> clients = new HashMap<String, Integer>();
 
     public ThreadPool(int minPoolSize, int maxPoolSize, String poolName, long keepAliveTime, ClassLoader classLoader, String objectName) {
         ThreadPoolExecutor p = new ThreadPoolExecutor(
             minPoolSize, // core size
             maxPoolSize, // max size
             keepAliveTime, TimeUnit.MILLISECONDS,
-            new SynchronousQueue());
+            new SynchronousQueue<Runnable>());
 
         p.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
         p.setThreadFactory(new ThreadPoolThreadFactory(poolName, classLoader));
-        
+
         try {
             this.objectName = ObjectName.getInstance(objectName);
         } catch (MalformedObjectNameException e) {
@@ -119,7 +120,7 @@ public class ThreadPool implements Geron
                 stats.prepareConsumers(clients);
             }
         } else {
-            stats.prepareConsumers(Collections.EMPTY_MAP);
+            stats.prepareConsumers(Collections.<String, Integer> emptyMap());
         }
         // set last sapmle time
         stats.setLastSampleTime();
@@ -142,7 +143,8 @@ public class ThreadPool implements Geron
         private BoundedRangeStatisticImpl threadsInUse = new BoundedRangeStatisticImpl(
                 "Threads In Use", "",
                 "The number of threads in use by this thread pool");
-        private Map consumers = new HashMap();
+
+        private Map<String, CountStatisticImpl> consumers = new HashMap<String, CountStatisticImpl>();
 
         public PoolStatsImpl() {
             addStat(threadsInUse.getName(), threadsInUse);
@@ -153,19 +155,19 @@ public class ThreadPool implements Geron
         }
 
         public CountStatistic getCountForConsumer(String consumer) {
-            return (CountStatistic) consumers.get(consumer);
+            return consumers.get(consumer);
         }
 
         public String[] getThreadConsumers() {
-            return (String[]) consumers.keySet().toArray(new String[consumers.size()]);
+            return consumers.keySet().toArray(new String[consumers.size()]);
         }
 
-        public void prepareConsumers(Map clients) {
-            Map result = new HashMap();
-            for (Iterator it = clients.keySet().iterator(); it.hasNext();) {
-                String client = (String) it.next();
-                Integer count = (Integer) clients.get(client);
-                CountStatisticImpl stat = (CountStatisticImpl) consumers.get(client);
+        public void prepareConsumers(Map<String, Integer> clients) {
+            Map<String, CountStatisticImpl> result = new HashMap<String, CountStatisticImpl>();
+            for (Map.Entry<String, Integer> entry : clients.entrySet()) {
+                String client = entry.getKey();
+                Integer count = entry.getValue();
+                CountStatisticImpl stat = consumers.get(client);
                 if (stat == null) {
                     stat = new CountStatisticImpl("Threads for " + client, "", "The number of threads used by the client known as '" + client + "'", count.intValue());
                     addStat(stat.getName(), stat);
@@ -175,9 +177,9 @@ public class ThreadPool implements Geron
                 }
                 result.put(client, stat);
             }
-            for (Iterator it = consumers.keySet().iterator(); it.hasNext();) {
-                String client = (String) it.next();
-                removeStat(((CountStatisticImpl) consumers.get(client)).getName());
+            for (Iterator<String> it = consumers.keySet().iterator(); it.hasNext();) {
+                String client = it.next();
+                removeStat((consumers.get(client)).getName());
             }
             consumers = result;
         }
@@ -203,11 +205,11 @@ public class ThreadPool implements Geron
     public void execute(Runnable command) {
         execute("Unknown", command);
     }
-    
+
     public void execute(Runnable command, long timeout, TimeUnit unit) {
         execute("Unknown", command, timeout, unit);
     }
-    
+
     public void execute(final String consumerName, final Runnable command, long timeout, TimeUnit unit) {
         if (waitWhenBlocked) {
             addToQueue(command, timeout, unit);
@@ -219,7 +221,7 @@ public class ThreadPool implements Geron
             }
         }
     }
-    
+
     private void addToQueue(Runnable command, long timeout, TimeUnit unit) {
         try {
             boolean added = executor.getQueue().offer(command, timeout, unit);
@@ -260,23 +262,23 @@ public class ThreadPool implements Geron
     }
 
     private synchronized void startWork(String consumerName) {
-        Integer test = (Integer) clients.get(consumerName);
+        Integer test = clients.get(consumerName);
         if (test == null) {
-            clients.put(consumerName, new Integer(1));
+            clients.put(consumerName, Integer.valueOf(1));
         } else {
-            clients.put(consumerName, new Integer(test.intValue() + 1));
+            clients.put(consumerName, Integer.valueOf(test.intValue() + 1));
         }
     }
 
     private synchronized void finishWork(String consumerName) {
-        Integer test = (Integer) clients.get(consumerName);
+        Integer test = clients.get(consumerName);
         if (test.intValue() == 1) {
             clients.remove(consumerName);
         } else {
-            clients.put(consumerName, new Integer(test.intValue() - 1));
+            clients.put(consumerName, Integer.valueOf(test.intValue() - 1));
         }
     }
-    
+
     private static class WaitWhenBlockedPolicy
         implements RejectedExecutionHandler
     {
@@ -289,7 +291,7 @@ public class ThreadPool implements Geron
             }
         }
     }
-    
+
     public void setWaitWhenBlocked(boolean wait) {
         waitWhenBlocked = wait;
         if(wait) {

Added: geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java?rev=995369&view=auto
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java (added)
+++ geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java Thu Sep  9 10:12:24 2010
@@ -0,0 +1,147 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.geronimo.pool;
+
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ThreadPoolTimeoutTest extends TestCase {
+
+    private ThreadPool threadPool;
+
+    private SleepRunnable firstRunnable;
+
+    private SleepRunnable secondRunnable;
+
+    public void setUp() throws Exception {
+        threadPool = new ThreadPool(1, 1, "abc", Long.MAX_VALUE, ThreadPoolTimeoutTest.class.getClassLoader(), "abc:pool=pool");
+        threadPool.doStart();
+        firstRunnable = new SleepRunnable(2000);
+        secondRunnable = new SleepRunnable(0);
+    }
+
+    public void testFailWaitWhenBlocked() {
+
+        threadPool.setWaitWhenBlocked(true);
+        // occupy the only thread in the pool
+        threadPool.execute(firstRunnable);
+
+        try {
+            // timeout is less than sleep time of firstRunnable
+            threadPool.execute(secondRunnable, 1, TimeUnit.SECONDS);
+            fail();
+        } catch (RejectedExecutionException e) {
+            // expected behavior
+            assertFalse(secondRunnable.started);
+        }
+    }
+
+    public void testFailAbortWhenBlocked() {
+
+        threadPool.setWaitWhenBlocked(false);
+
+        // occupy the only thread in the pool
+        threadPool.execute(firstRunnable);
+
+        try {
+            // timeout is less than sleep time of firstRunnable
+            threadPool.execute(secondRunnable, 1, TimeUnit.SECONDS);
+            fail();
+        } catch (RejectedExecutionException e) {
+            // expected behavior
+            assertFalse(secondRunnable.started);
+        }
+    }
+
+    public void testSuccessWaitWhenBlocked() {
+
+        threadPool.setWaitWhenBlocked(true);
+
+        // occupy the only thread in the pool
+        threadPool.execute(firstRunnable);
+
+        try {
+            // timeout is more than sleep time of firstRunnable, so secondRunnable will get executed
+            threadPool.execute(secondRunnable, 3, TimeUnit.SECONDS);
+            try {
+                Thread.sleep(3000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            assertTrue(secondRunnable.started);
+        } catch (RejectedExecutionException e) {
+            fail();
+        }
+    }
+
+    public void testSuccessAbortWhenBlocked() {
+
+        threadPool.setWaitWhenBlocked(false);
+
+        // occupy the only thread in the pool
+        threadPool.execute(firstRunnable);
+
+        try {
+            // timeout is more than sleep time of firstRunnable, so secondRunnable will get executed
+            threadPool.execute(secondRunnable, 3, TimeUnit.SECONDS);
+            try {
+                Thread.sleep(3000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            assertTrue(secondRunnable.started);
+        } catch (RejectedExecutionException e) {
+            fail();
+        }
+    }
+
+    class SleepRunnable implements Runnable {
+
+        private long time;
+
+        boolean started;
+
+        boolean ended;
+
+        SleepRunnable(long t) {
+            this.time = t;
+            started = false;
+            ended = false;
+        }
+
+        public void run() {
+            try {
+                started = true;
+                Thread.sleep(time);
+                ended = true;
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void tearDown() throws Exception {
+        Thread.sleep(3000);
+        threadPool.doStop();
+    }
+}

Propchange: geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/server/trunk/framework/modules/geronimo-core/src/test/java/org/apache/geronimo/pool/ThreadPoolTimeoutTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain