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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.istack.internal;
    28 import java.util.concurrent.ConcurrentLinkedQueue;
    29 import java.lang.ref.WeakReference;
    31 /**
    32  * Pool of reusable objects that are indistinguishable from each other,
    33  * such as JAXB marshallers.
    34  *
    35  * @author Kohsuke Kawaguchi
    36  */
    37 public interface Pool<T> {
    39     /**
    40      * Gets a new object from the pool.
    41      *
    42      * <p>
    43      * If no object is available in the pool, this method creates a new one.
    44      */
    45     @NotNull T take();
    47     /**
    48      * Returns an object back to the pool.
    49      */
    50     void recycle(@NotNull T t);
    52     /**
    53      * Default implementation that uses {@link ConcurrentLinkedQueue}
    54      * as the data store.
    55      *
    56      * <h2>Note for Implementors</h2>
    57      * <p>
    58      * Don't rely on the fact that this class extends from {@link ConcurrentLinkedQueue}.
    59      */
    60     public abstract class Impl<T> implements Pool<T> {
    62         private volatile WeakReference<ConcurrentLinkedQueue<T>> queue;
    64         /**
    65          * Gets a new object from the pool.
    66          *
    67          * <p>
    68          * If no object is available in the pool, this method creates a new one.
    69          *
    70          * @return
    71          *      always non-null.
    72          */
    73         public final @NotNull T take() {
    74             T t = getQueue().poll();
    75             if(t==null) {
    76                 return create();
    77             }
    78             return t;
    79         }
    81         /**
    82          * Returns an object back to the pool.
    83          */
    84         public final void recycle(T t) {
    85             getQueue().offer(t);
    86         }
    88         private ConcurrentLinkedQueue<T> getQueue() {
    89             WeakReference<ConcurrentLinkedQueue<T>> q = queue;
    90             if (q != null) {
    91                 ConcurrentLinkedQueue<T> d = q.get();
    92                 if (d != null) {
    93                     return d;
    94                 }
    95             }
    96             // overwrite the queue
    97             ConcurrentLinkedQueue<T> d = new ConcurrentLinkedQueue<T>();
    98             queue = new WeakReference<ConcurrentLinkedQueue<T>>(d);
   100             return d;
   101         }
   103         /**
   104          * Creates a new instance of object.
   105          *
   106          * <p>
   107          * This method is used when someone wants to
   108          * {@link #take() take} an object from an empty pool.
   109          *
   110          * <p>
   111          * Also note that multiple threads may call this method
   112          * concurrently.
   113          */
   114         protected abstract @NotNull T create();
   115     }
   116 }

mercurial