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

com.jdon.util.ConcurrentLinkedList Maven / Gradle / Ivy

Go to download

JdonFramework is a java framework that you can use to build your Domain Driven Design + CQRS + EventSource applications with asynchronous concurrency and higher throughput.

There is a newer version: 6.9
Show newest version
package com.jdon.util;

import java.util.LinkedList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ConcurrentLinkedList {
	private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(
			true);

	private final Lock readLock = readWriteLock.readLock();

	private final Lock writeLock = readWriteLock.writeLock();

	/** A list of the elements order by Least Recent Use */
	public final LinkedList keyLRUList = new LinkedList();

	public void add(Object o) {
		writeLock.lock();
		try {
			keyLRUList.add(o);
		} finally {
			writeLock.unlock();
		}
	}

	public void addFirst(Object key) {
		writeLock.lock();
		try {
			keyLRUList.addFirst(key);
		} finally {
			writeLock.unlock();
		}
	}

	public void moveFirst(Object key) {
		writeLock.lock();
		try {
			keyLRUList.remove(key);
			keyLRUList.addFirst(key);
		} catch (Exception e) {
		} finally {
			writeLock.unlock();
		}
	}

	public Object getLast() {
		readLock.lock();
		try {
			return keyLRUList.getLast();
		} finally {
			readLock.unlock();
		}
	}

	public int size() {
		readLock.lock();
		try {
			return keyLRUList.size();
		} finally {
			readLock.unlock();
		}
	}

	public void remove(Object key) {
		readLock.lock();
		try {
			keyLRUList.remove(key);
		} finally {
			readLock.unlock();
		}
	}

	public void clear() {
		writeLock.lock();
		try {
			keyLRUList.clear();
		} finally {
			writeLock.unlock();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy