01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.jaxws.sample.parallelasync.common;
21:
22: import java.util.concurrent.ArrayBlockingQueue;
23: import java.util.concurrent.ThreadPoolExecutor;
24: import java.util.concurrent.TimeUnit;
25: import java.util.concurrent.locks.Condition;
26: import java.util.concurrent.locks.ReentrantLock;
27:
28: /**
29: * A custom executor that can be paused for testing of exceptions that occur
30: * before an item is executed. This executor provides absolue control over its
31: * thread size via size parameter in the constructor
32: */
33: public class PausableExecutor extends ThreadPoolExecutor {
34: private boolean isPaused;
35:
36: private ReentrantLock pauseLock = new ReentrantLock();
37:
38: private Condition unpaused = pauseLock.newCondition();
39:
40: public PausableExecutor(int size) {
41: super (size, size, 1, TimeUnit.SECONDS,
42: new ArrayBlockingQueue<Runnable>(size));
43: }
44:
45: protected void beforeExecute(Thread t, Runnable r) {
46: super .beforeExecute(t, r);
47: pauseLock.lock();
48: try {
49: while (isPaused)
50: unpaused.await();
51: } catch (InterruptedException ie) {
52: t.interrupt();
53: } finally {
54: pauseLock.unlock();
55: }
56: }
57:
58: public void pause() {
59: pauseLock.lock();
60: try {
61: isPaused = true;
62: } finally {
63: pauseLock.unlock();
64: }
65: }
66:
67: public void resume() {
68: pauseLock.lock();
69: try {
70: isPaused = false;
71: unpaused.signalAll();
72: } finally {
73: pauseLock.unlock();
74: }
75: }
76: }
|