src/share/jaxws_classes/com/sun/istack/internal/Pool.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/istack/internal/Pool.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,116 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.istack.internal;
    1.30 +
    1.31 +import java.util.concurrent.ConcurrentLinkedQueue;
    1.32 +import java.lang.ref.WeakReference;
    1.33 +
    1.34 +/**
    1.35 + * Pool of reusable objects that are indistinguishable from each other,
    1.36 + * such as JAXB marshallers.
    1.37 + *
    1.38 + * @author Kohsuke Kawaguchi
    1.39 + */
    1.40 +public interface Pool<T> {
    1.41 +
    1.42 +    /**
    1.43 +     * Gets a new object from the pool.
    1.44 +     *
    1.45 +     * <p>
    1.46 +     * If no object is available in the pool, this method creates a new one.
    1.47 +     */
    1.48 +    @NotNull T take();
    1.49 +
    1.50 +    /**
    1.51 +     * Returns an object back to the pool.
    1.52 +     */
    1.53 +    void recycle(@NotNull T t);
    1.54 +
    1.55 +    /**
    1.56 +     * Default implementation that uses {@link ConcurrentLinkedQueue}
    1.57 +     * as the data store.
    1.58 +     *
    1.59 +     * <h2>Note for Implementors</h2>
    1.60 +     * <p>
    1.61 +     * Don't rely on the fact that this class extends from {@link ConcurrentLinkedQueue}.
    1.62 +     */
    1.63 +    public abstract class Impl<T> implements Pool<T> {
    1.64 +
    1.65 +        private volatile WeakReference<ConcurrentLinkedQueue<T>> queue;
    1.66 +
    1.67 +        /**
    1.68 +         * Gets a new object from the pool.
    1.69 +         *
    1.70 +         * <p>
    1.71 +         * If no object is available in the pool, this method creates a new one.
    1.72 +         *
    1.73 +         * @return
    1.74 +         *      always non-null.
    1.75 +         */
    1.76 +        public final @NotNull T take() {
    1.77 +            T t = getQueue().poll();
    1.78 +            if(t==null) {
    1.79 +                return create();
    1.80 +            }
    1.81 +            return t;
    1.82 +        }
    1.83 +
    1.84 +        /**
    1.85 +         * Returns an object back to the pool.
    1.86 +         */
    1.87 +        public final void recycle(T t) {
    1.88 +            getQueue().offer(t);
    1.89 +        }
    1.90 +
    1.91 +        private ConcurrentLinkedQueue<T> getQueue() {
    1.92 +            WeakReference<ConcurrentLinkedQueue<T>> q = queue;
    1.93 +            if (q != null) {
    1.94 +                ConcurrentLinkedQueue<T> d = q.get();
    1.95 +                if (d != null) {
    1.96 +                    return d;
    1.97 +                }
    1.98 +            }
    1.99 +            // overwrite the queue
   1.100 +            ConcurrentLinkedQueue<T> d = new ConcurrentLinkedQueue<T>();
   1.101 +            queue = new WeakReference<ConcurrentLinkedQueue<T>>(d);
   1.102 +
   1.103 +            return d;
   1.104 +        }
   1.105 +
   1.106 +        /**
   1.107 +         * Creates a new instance of object.
   1.108 +         *
   1.109 +         * <p>
   1.110 +         * This method is used when someone wants to
   1.111 +         * {@link #take() take} an object from an empty pool.
   1.112 +         *
   1.113 +         * <p>
   1.114 +         * Also note that multiple threads may call this method
   1.115 +         * concurrently.
   1.116 +         */
   1.117 +        protected abstract @NotNull T create();
   1.118 +    }
   1.119 +}

mercurial