// Tests that a listener is only invoked by a single thread at any time even if multiple threads are // invoking the wrapper concurrently. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void concurrentExecution() throws InterruptedException public virtual void concurrentExecution() { int nThreads = Runtime.Runtime.availableProcessors(); int resultsPerThread = 10; ConcurrentLinkedQueue <string> errors = new ConcurrentLinkedQueue <string>(); System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(1); int expectedResultCount = nThreads * resultsPerThread; Listener listener = new Listener(errors, latch); System.Action <CalculationResults> wrapper = new ListenerWrapper(listener, expectedResultCount, ImmutableList.of(), ImmutableList.of()); ExecutorService executor = Executors.newFixedThreadPool(nThreads); CalculationResult result = CalculationResult.of(0, 0, Result.failure(FailureReason.ERROR, "foo")); CalculationTarget target = new CalculationTargetAnonymousInnerClass(this); CalculationResults results = CalculationResults.of(target, ImmutableList.of(result)); IntStream.range(0, expectedResultCount).forEach(i => executor.submit(() => wrapper(results))); latch.await(); executor.shutdown(); if (!errors.Empty) { //JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter: string allErrors = errors.collect(joining("\n")); fail(allErrors); } }
public void calculateMultiScenarioAsync(CalculationTasks tasks, ScenarioMarketData marketData, ReferenceData refData, CalculationListener listener) { IList <CalculationTask> taskList = tasks.Tasks; // the listener is invoked via this wrapper // the wrapper ensures thread-safety for the listener // it also calls the listener with single CalculationResult cells, not CalculationResults System.Action <CalculationResults> consumer = new ListenerWrapper(listener, taskList.Count, tasks.Targets, tasks.Columns); // run each task using the executor taskList.ForEach(task => runTask(task, marketData, refData, consumer)); }