co.paralleluniverse.common.benchmark.Striped Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quasar-core Show documentation
Show all versions of quasar-core Show documentation
Fibers, Channels and Actors for the JVM
The newest version!
/*
* Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
package co.paralleluniverse.common.benchmark;
import co.paralleluniverse.concurrent.util.MapUtil;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentMap;
/**
*
* @author pron
*/
public abstract class Striped implements Iterable {
private final ConcurrentMap rs;
public Striped() {
this.rs = MapUtil.newConcurrentHashMap();
}
public Collection combine() {
return rs.values();
}
public T get() {
Thread thread = Thread.currentThread();
T r = rs.get(thread);
if (r == null) {
r = newResource();
rs.put(thread, r);
}
return r;
}
protected abstract T newResource();
@Override
public Iterator iterator() {
return rs.values().iterator();
}
public int size() {
return rs.size();
}
}