From scm-return-43981-apmail-geronimo-scm-archive=geronimo.apache.org@geronimo.apache.org Thu Sep 09 10:13:11 2010 Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 79819 invoked from network); 9 Sep 2010 10:13:11 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 9 Sep 2010 10:13:11 -0000 Received: (qmail 81727 invoked by uid 500); 9 Sep 2010 10:13:11 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 81601 invoked by uid 500); 9 Sep 2010 10:13:09 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 81589 invoked by uid 99); 9 Sep 2010 10:13:09 -0000 Received: from Unknown (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Sep 2010 10:13:09 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Sep 2010 10:12:47 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5B2A723889B2; Thu, 9 Sep 2010 10:12:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Thu, 09 Sep 2010 10:12:25 -0000 To: scm@geronimo.apache.org From: xuhaihong@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100909101225.5B2A723889B2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 clients = new HashMap(); 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()); 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. 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 consumers = new HashMap(); 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 clients) { + Map result = new HashMap(); + for (Map.Entry 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 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