net.sf.javagimmicks.concurrent.locks.DefaultLockRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks-concurrent Show documentation
Show all versions of gimmicks-concurrent Show documentation
Utilities for multi-threaded programming
The newest version!
package net.sf.javagimmicks.concurrent.locks;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import net.sf.javagimmicks.util.Supplier;
class DefaultLockRegistry implements LockRegistry, Serializable
{
private static final long serialVersionUID = -785304313135882910L;
private static final Supplier> HASHSET_SHARED_COLLECTION_SUPPLIER = new HashSetSharedCollectionSupplier();
public static DefaultLockRegistry createHashBasedInstance()
{
return new DefaultLockRegistry(new HashMap(), new HashMap>(),
HASHSET_SHARED_COLLECTION_SUPPLIER);
}
public static DefaultLockRegistry createTreeBasedInstance()
{
return new DefaultLockRegistry(new TreeMap(), new TreeMap>(),
HASHSET_SHARED_COLLECTION_SUPPLIER);
}
protected final Map _exRegistry;
protected final Map> _shRegistry;
protected final Supplier> _shCollectionSupplier;
public DefaultLockRegistry(final Map exRegistry, final Map> shRegistry,
final Supplier> sharedCollectionFactory)
{
_exRegistry = exRegistry;
_shRegistry = shRegistry;
_shCollectionSupplier = sharedCollectionFactory;
}
@Override
public boolean isSharedFree(final Collection resources)
{
for (final K resource : resources)
{
if (_shRegistry.containsKey(resource))
{
return false;
}
}
return true;
}
@Override
public void registerShared(final Collection resources)
{
final Thread currentThread = Thread.currentThread();
for (final K resource : resources)
{
Collection threadsForResource = _shRegistry.get(resource);
if (threadsForResource == null)
{
threadsForResource = _shCollectionSupplier.get();
_shRegistry.put(resource, threadsForResource);
}
threadsForResource.add(currentThread);
}
}
@Override
public void unregisterShared(final Collection resources)
{
final Thread currentThread = Thread.currentThread();
for (final K resource : resources)
{
final Collection threadsForResource = _shRegistry.get(resource);
if (threadsForResource == null)
{
continue;
}
threadsForResource.remove(currentThread);
if (threadsForResource.isEmpty())
{
_shRegistry.remove(resource);
}
}
}
@Override
public boolean isExclusiveFree(final Collection resources)
{
for (final K resource : resources)
{
if (_exRegistry.containsKey(resource))
{
return false;
}
}
return true;
}
@Override
public void registerExclusive(final Collection resources)
{
final Thread currentThread = Thread.currentThread();
for (final K resource : resources)
{
_exRegistry.put(resource, currentThread);
}
}
@Override
public void unregisterExclusive(final Collection resources)
{
for (final K resource : resources)
{
_exRegistry.remove(resource);
}
}
@Override
public void dump(final Writer out) throws IOException
{
final PrintWriter pw = new PrintWriter(out);
pw.println();
pw.println("Exclusive Lock Registry entries:");
for (final Entry entry : _exRegistry.entrySet())
{
final String resource = entry.getKey().toString();
final Thread thread = entry.getValue();
pw.println(String.format("\t-Resource: %s is locked by Thread with name: '%s' and id: '%d'",
resource,
thread.getName(),
thread.getId()));
}
pw.println();
pw.println("Shared Lock Registry entries:");
for (final Entry> entry : _shRegistry.entrySet())
{
final String resource = entry.getKey().toString();
final Collection threads = entry.getValue();
pw.println(String.format("\t-Resource: %s is locked by:", resource));
for (final Thread thread : threads)
{
pw.println(String.format("\t\tThread with name: '%s' and id: '%d'", thread.getName(), thread.getId()));
}
}
pw.flush();
}
private static final class HashSetSharedCollectionSupplier implements Supplier>, Serializable
{
private static final long serialVersionUID = 3613642441786733902L;
@Override
public Collection get()
{
return new HashSet();
}
}
}