All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.fw.common.mithra.cache.MithraReferenceThread Maven / Gradle / Ivy

There is a newer version: 18.1.0
Show newest version
/*
 Copyright 2016 Goldman Sachs.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

package com.gs.fw.common.mithra.cache;

import com.gs.collections.impl.list.mutable.FastList;
import com.gs.fw.common.mithra.extractor.IdentityExtractor;
import com.gs.fw.common.mithra.util.MithraFastList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.lang.ref.WeakReference;


public class MithraReferenceThread extends Thread
{
    private static final Logger logger = LoggerFactory.getLogger(MithraReferenceThread.class);

    private static final MithraReferenceThread instance = new MithraReferenceThread();

    private final MithraFastList listeners = new MithraFastList();
    private final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();
    private final ConcurrentLinkedQueue removeQueue = new ConcurrentLinkedQueue();

    static
    {
        instance.start();
    }

    public MithraReferenceThread()
    {
        super("MithraReferenceThread");
        this.setDaemon(true);
    }

    public static MithraReferenceThread getInstance()
    {
        return instance;
    }

    public void addListener(ReferenceListener listener)
    {
        queue.add(new WeakReferenceListener(listener));
    }

    @Override
    public void run()
    {
        while(true)
        {
            if (this.listeners == null)
            {
                // only happens when badly written container code changes our private final variables
                logger.error("Detected misbehaving container and shutting down MithraReferenceThread");
                break;
            }
            try
            {
                WeakReferenceListener toAdd = queue.poll();
                while (toAdd != null)
                {
                    listeners.add(toAdd);
                    toAdd = queue.poll();
                }
                FullUniqueIndex set = null;
                ReferenceListener toRemove = removeQueue.poll();
                while(toRemove != null)
                {
                    if (set == null)
                    {
                        set = new FullUniqueIndex("", IdentityExtractor.getArrayInstance());
                    }
                    set.put(toRemove);
                    toRemove = removeQueue.poll();
                }
                for(int i=0;i
    {
        private WeakReferenceListener(ReferenceListener referent)
        {
            super(referent);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy