use of java.util.concurrent.CopyOnWriteArrayList in project azure-sdk-for-java by Azure.
the class TaskOperations method createTasks.
/**
* Adds multiple tasks to a job.
*
* @param jobId The ID of the job to which to add the task.
* @param taskList A collection of {@link CloudTask tasks} to add.
* @param additionalBehaviors A collection of {@link BatchClientBehavior} instances that are applied to the Batch service request.
* @throws BatchErrorException Exception thrown from REST call
* @throws IOException Exception thrown from serialization/deserialization
* @throws InterruptedException exception thrown if any thread has interrupted the current thread.
*/
public void createTasks(String jobId, List<TaskAddParameter> taskList, Iterable<BatchClientBehavior> additionalBehaviors) throws BatchErrorException, IOException, InterruptedException {
BehaviorManager bhMgr = new BehaviorManager(this.customBehaviors(), additionalBehaviors);
// Default thread number is 1
int threadNumber = 1;
// Get user defined thread number
for (BatchClientBehavior op : bhMgr.getMasterListOfBehaviors()) {
if (op instanceof BatchClientParallelOptions) {
threadNumber = ((BatchClientParallelOptions) op).maxDegreeOfParallelism();
break;
}
}
final Object lock = new Object();
ConcurrentLinkedQueue<TaskAddParameter> pendingList = new ConcurrentLinkedQueue<>(taskList);
CopyOnWriteArrayList<TaskAddResult> failures = new CopyOnWriteArrayList<>();
Map<Thread, WorkingThread> threads = new HashMap<>();
Exception innerException = null;
while (!pendingList.isEmpty()) {
if (threads.size() < threadNumber) {
// Kick as many as possible add tasks requests by max allowed threads
WorkingThread worker = new WorkingThread(this._parentBatchClient, bhMgr, jobId, pendingList, failures, lock);
Thread thread = new Thread(worker);
thread.start();
threads.put(thread, worker);
} else {
// Wait any thread finished
synchronized (lock) {
lock.wait();
}
List<Thread> finishedThreads = new ArrayList<>();
for (Thread t : threads.keySet()) {
if (t.getState() == Thread.State.TERMINATED) {
finishedThreads.add(t);
// If any exception happened, do not care the left requests
innerException = threads.get(t).getException();
if (innerException != null) {
break;
}
}
}
// Free thread pool, so we can start more threads to send the request
threads.keySet().removeAll(finishedThreads);
// Any errors happened, we stop
if (innerException != null || !failures.isEmpty()) {
break;
}
}
}
// May sure all the left threads finished
for (Thread t : threads.keySet()) {
t.join();
}
if (innerException == null) {
// Anything bad happened at the left threads?
for (Thread t : threads.keySet()) {
innerException = threads.get(t).getException();
if (innerException != null) {
break;
}
}
}
if (innerException != null) {
// We throw any exception happened in sub thread
if (innerException instanceof BatchErrorException) {
throw (BatchErrorException) innerException;
} else {
throw (IOException) innerException;
}
}
if (!failures.isEmpty()) {
// Report any client error with leftover request
List<TaskAddParameter> notFinished = new ArrayList<>();
for (TaskAddParameter param : pendingList) {
notFinished.add(param);
}
throw new CreateTasksTerminatedException("At least one task failed to be added.", failures, notFinished);
}
// We succeed here
}
use of java.util.concurrent.CopyOnWriteArrayList in project intellij-community by JetBrains.
the class RendererConfiguration method clone.
public RendererConfiguration clone() {
RendererConfiguration result = null;
try {
result = (RendererConfiguration) super.clone();
} catch (CloneNotSupportedException e) {
LOG.error(e);
}
result.myRepresentationNodes = new CopyOnWriteArrayList<>();
final ArrayList<NodeRenderer> cloned = new ArrayList<>();
for (NodeRenderer renderer : myRepresentationNodes) {
cloned.add((NodeRenderer) renderer.clone());
}
result.setRenderers(cloned);
return result;
}
use of java.util.concurrent.CopyOnWriteArrayList in project AntennaPod by AntennaPod.
the class CastManager method onQueueUpdated.
/*
* This is called by onQueueStatusUpdated() of RemoteMediaPlayer
*/
private void onQueueUpdated(List<MediaQueueItem> queueItems, MediaQueueItem item, int repeatMode, boolean shuffle) {
Log.d(TAG, "onQueueUpdated() reached");
Log.d(TAG, String.format("Queue Items size: %d, Item: %s, Repeat Mode: %d, Shuffle: %s", queueItems == null ? 0 : queueItems.size(), item, repeatMode, shuffle));
if (queueItems != null) {
mediaQueue = new MediaQueue(new CopyOnWriteArrayList<>(queueItems), item, shuffle, repeatMode);
} else {
mediaQueue = new MediaQueue(new CopyOnWriteArrayList<>(), null, false, MediaStatus.REPEAT_MODE_REPEAT_OFF);
}
for (CastConsumer consumer : castConsumers) {
consumer.onMediaQueueUpdated(queueItems, item, repeatMode, shuffle);
}
}
use of java.util.concurrent.CopyOnWriteArrayList in project lucene-solr by apache.
the class FastLRUCache method initializeMetrics.
@Override
public void initializeMetrics(SolrMetricManager manager, String registryName, String scope) {
registry = manager.registry(registryName);
cacheMap = new MetricsMap((detailed, map) -> {
if (cache != null) {
ConcurrentLRUCache.Stats stats = cache.getStats();
long lookups = stats.getCumulativeLookups();
long hits = stats.getCumulativeHits();
long inserts = stats.getCumulativePuts();
long evictions = stats.getCumulativeEvictions();
long size = stats.getCurrentSize();
long clookups = 0;
long chits = 0;
long cinserts = 0;
long cevictions = 0;
// NOTE: It is safe to iterate on a CopyOnWriteArrayList
for (ConcurrentLRUCache.Stats statistiscs : statsList) {
clookups += statistiscs.getCumulativeLookups();
chits += statistiscs.getCumulativeHits();
cinserts += statistiscs.getCumulativePuts();
cevictions += statistiscs.getCumulativeEvictions();
}
map.put("lookups", lookups);
map.put("hits", hits);
map.put("hitratio", calcHitRatio(lookups, hits));
map.put("inserts", inserts);
map.put("evictions", evictions);
map.put("size", size);
map.put("warmupTime", warmupTime);
map.put("cumulative_lookups", clookups);
map.put("cumulative_hits", chits);
map.put("cumulative_hitratio", calcHitRatio(clookups, chits));
map.put("cumulative_inserts", cinserts);
map.put("cumulative_evictions", cevictions);
if (detailed && showItems != 0) {
Map items = cache.getLatestAccessedItems(showItems == -1 ? Integer.MAX_VALUE : showItems);
for (Map.Entry e : (Set<Map.Entry>) items.entrySet()) {
Object k = e.getKey();
Object v = e.getValue();
String ks = "item_" + k;
String vs = v.toString();
map.put(ks, vs);
}
}
}
});
manager.registerGauge(this, registryName, cacheMap, true, scope, getCategory().toString());
}
use of java.util.concurrent.CopyOnWriteArrayList in project lucene-solr by apache.
the class TestLockTree method testLocks.
public void testLocks() throws Exception {
LockTree lockTree = new LockTree();
Lock coll1Lock = lockTree.getSession().lock(CollectionAction.CREATE, Arrays.asList("coll1"));
assertNotNull(coll1Lock);
assertNull("Should not be able to lock coll1/shard1", lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1")));
assertNull(lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
coll1Lock.unlock();
Lock shard1Lock = lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1"));
assertNotNull(shard1Lock);
shard1Lock.unlock();
Lock replica1Lock = lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2"));
assertNotNull(replica1Lock);
List<Pair<CollectionAction, List<String>>> operations = new ArrayList<>();
operations.add(new Pair<>(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll1")));
operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll1", "shard1")));
operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll2", "shard2")));
operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll2")));
operations.add(new Pair<>(DELETEREPLICA, Arrays.asList("coll2", "shard1")));
List<Set<String>> orderOfExecution = Arrays.asList(ImmutableSet.of("coll1/shard1/core_node2", "coll2/shard2"), ImmutableSet.of("coll1", "coll2"), ImmutableSet.of("coll1/shard1", "coll2/shard1"));
lockTree = new LockTree();
for (int counter = 0; counter < orderOfExecution.size(); counter++) {
LockTree.Session session = lockTree.getSession();
List<Pair<CollectionAction, List<String>>> completedOps = new CopyOnWriteArrayList<>();
List<Lock> locks = new CopyOnWriteArrayList<>();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < operations.size(); i++) {
Pair<CollectionAction, List<String>> operation = operations.get(i);
final Lock lock = session.lock(operation.first(), operation.second());
if (lock != null) {
Thread thread = new Thread(getRunnable(completedOps, operation, locks, lock));
threads.add(thread);
thread.start();
}
}
for (Thread thread : threads) thread.join();
if (locks.isEmpty())
throw new RuntimeException("Could not attain lock for anything " + operations);
Set<String> expectedOps = orderOfExecution.get(counter);
log.info("counter : {} , expected : {}, actual : {}", counter, expectedOps, locks);
assertEquals(expectedOps.size(), locks.size());
for (Lock lock : locks) assertTrue("locks : " + locks + " expectedOps : " + expectedOps, expectedOps.contains(lock.toString()));
locks.clear();
for (Pair<CollectionAction, List<String>> completedOp : completedOps) {
operations.remove(completedOp);
}
}
}
Aggregations