Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WaitPolicy |
|
| 2.3333333333333335;2.333 |
1 | /* | |
2 | * $Id: WaitPolicy.java 8993 2007-10-08 13:50:23Z holger $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | ||
11 | package org.mule.util.concurrent; | |
12 | ||
13 | import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException; | |
14 | import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler; | |
15 | import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; | |
16 | import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; | |
17 | ||
18 | /** | |
19 | * A handler for unexecutable tasks that waits until the task can be submitted for | |
20 | * execution or times out. Generously snipped from the jsr166 repository at: <a | |
21 | * href="http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ThreadPoolExecutor.java"></a>. | |
22 | */ | |
23 | // @Immutable | |
24 | public class WaitPolicy implements RejectedExecutionHandler | |
25 | { | |
26 | private final long time; | |
27 | private final TimeUnit timeUnit; | |
28 | ||
29 | /** | |
30 | * Constructs a <tt>WaitPolicy</tt> which waits (almost) forever. | |
31 | */ | |
32 | public WaitPolicy() | |
33 | { | |
34 | // effectively waits forever | |
35 | 2 | this(Long.MAX_VALUE, TimeUnit.SECONDS); |
36 | 2 | } |
37 | ||
38 | /** | |
39 | * Constructs a <tt>WaitPolicy</tt> with timeout. A negative <code>time</code> | |
40 | * value is interpreted as <code>Long.MAX_VALUE</code>. | |
41 | */ | |
42 | public WaitPolicy(long time, TimeUnit timeUnit) | |
43 | { | |
44 | 8 | super(); |
45 | 8 | this.time = (time < 0 ? Long.MAX_VALUE : time); |
46 | 8 | this.timeUnit = timeUnit; |
47 | 8 | } |
48 | ||
49 | public void rejectedExecution(Runnable r, ThreadPoolExecutor e) | |
50 | { | |
51 | try | |
52 | { | |
53 | 8 | if (e.isShutdown() || !e.getQueue().offer(r, time, timeUnit)) |
54 | { | |
55 | // TODO better message | |
56 | 4 | throw new RejectedExecutionException(); |
57 | } | |
58 | } | |
59 | 0 | catch (InterruptedException ie) |
60 | { | |
61 | 0 | Thread.currentThread().interrupt(); |
62 | 0 | throw new RejectedExecutionException(ie); |
63 | 4 | } |
64 | 4 | } |
65 | ||
66 | } |