Path: blob/master/src/java.desktop/share/classes/sun/java2d/ReentrantContextProviderCLQ.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.java2d;2526import java.lang.ref.Reference;27import java.util.concurrent.ConcurrentLinkedQueue;2829/**30* This ReentrantContextProvider implementation uses one ConcurrentLinkedQueue31* to store all ReentrantContext instances (thread and its child contexts)32*33* Note: this implementation keeps less contexts in memory depending on the34* concurrent active threads in contrary to a ThreadLocal provider. However,35* it is slower in highly concurrent workloads.36*37* @param <K> ReentrantContext subclass38*/39public abstract class ReentrantContextProviderCLQ<K extends ReentrantContext>40extends ReentrantContextProvider<K>41{42// ReentrantContext queue to store all contexts43private final ConcurrentLinkedQueue<Reference<K>> ctxQueue44= new ConcurrentLinkedQueue<Reference<K>>();4546/**47* Create a new ReentrantContext provider using the given reference type48* among hard, soft or weak based using a ConcurrentLinkedQueue storage49*50* @param refType reference type51*/52public ReentrantContextProviderCLQ(final int refType) {53super(refType);54}5556/**57* Give a ReentrantContext instance for the current thread58*59* @return ReentrantContext instance60*/61@Override62public final K acquire() {63K ctx = null;64// Drain queue if all referent are null:65Reference<K> ref = null;66while ((ctx == null) && ((ref = ctxQueue.poll()) != null)) {67ctx = ref.get();68}69if (ctx == null) {70// create a new ReentrantContext if none is available71ctx = newContext();72ctx.usage = USAGE_CLQ;73}74return ctx;75}7677/**78* Restore the given ReentrantContext instance for reuse79*80* @param ctx ReentrantContext instance81*/82@Override83public final void release(final K ctx) {84if (ctx.usage == USAGE_CLQ) {85ctxQueue.offer(getOrCreateReference(ctx));86}87}88}899091