01: package com.sun.istack;
02:
03: import java.util.concurrent.ConcurrentLinkedQueue;
04:
05: /**
06: * Pool of reusable objects that are indistinguishable from each other,
07: * such as JAXB marshallers.
08: *
09: * @author Kohsuke Kawaguchi
10: */
11: public interface Pool<T> {
12: /**
13: * Gets a new object from the pool.
14: *
15: * <p>
16: * If no object is available in the pool, this method creates a new one.
17: */
18: @NotNull
19: T take();
20:
21: /**
22: * Returns an object back to the pool.
23: */
24: void recycle(@NotNull
25: T t);
26:
27: /**
28: * Default implementation that uses {@link ConcurrentLinkedQueue}
29: * as the data store.
30: *
31: * <h2>Note for Implementors</h2>
32: * <p>
33: * Don't rely on the fact that this class extends from {@link ConcurrentLinkedQueue}.
34: */
35: public abstract class Impl<T> extends ConcurrentLinkedQueue<T>
36: implements Pool<T> {
37: /**
38: * Gets a new object from the pool.
39: *
40: * <p>
41: * If no object is available in the pool, this method creates a new one.
42: *
43: * @return
44: * always non-null.
45: */
46: public final @NotNull
47: T take() {
48: T t = super .poll();
49: if (t == null)
50: return create();
51: return t;
52: }
53:
54: /**
55: * Returns an object back to the pool.
56: */
57: public final void recycle(T t) {
58: super .offer(t);
59: }
60:
61: /**
62: * Creates a new instance of object.
63: *
64: * <p>
65: * This method is used when someone wants to
66: * {@link #take() take} an object from an empty pool.
67: *
68: * <p>
69: * Also note that multiple threads may call this method
70: * concurrently.
71: */
72: protected abstract @NotNull
73: T create();
74: }
75: }
|