Search in sources :

Example 16 with ConcurrentRuntimeException

use of org.apache.commons.lang3.concurrent.ConcurrentRuntimeException in project GDSC-SMLM by aherbert.

the class FitEngine method end.

/**
 * Signal that no more fitting work will be added to the queue.
 *
 * <p>Ask all threads to end and wait. Returns when all threads have stopped running.
 *
 * @param now Stop the work immediately, otherwise finish all work in the queue
 * @throws ConcurrentRuntimeException if interrupted while waiting to add.
 */
public synchronized void end(boolean now) {
    if (threads.isEmpty()) {
        return;
    }
    time = 0;
    if (now) {
        // Request worker shutdown
        for (final FitWorker worker : workers) {
            worker.finish();
        }
        // If there are already jobs then the worker will stop due to the finish() signal.
        for (int i = 0; i < threads.size(); i++) {
            // non-blocking add to queue
            if (!jobs.offer(EMPTY_JOB)) {
                // At capacity so stop adding more
                break;
            }
        }
    } else {
        // Finish all the worker threads by passing in a null job
        for (int i = 0; i < threads.size(); i++) {
            // blocking add to queue
            put(EMPTY_JOB);
        }
    }
    // Collect all the threads
    for (int i = 0; i < threads.size(); i++) {
        try {
            threads.get(i).join();
            time += workers.get(i).getTime();
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Unexpected interruption", ex);
            throw new ConcurrentRuntimeException(ex);
        }
    }
    // Output this to the log
    if (counter != null) {
        // Get the stats we want...
        // Note: The total localisations may be less than 'fit single' + 2 * 'fit doublet':
        // - Duplicates are eliminated from the results
        // - Doublet fitting may result in excluded spots due to bad shifts, fitting an existing
        // result or fitting a candidate which creates an estimate for re-use when the candidate
        // is processed.
        // System.out.println(results.getName()); // Dataset name
        logger.info("Fitting paths...");
        final int total = counter.getTotal();
        final int single = counter.getUnset(FitType.MULTI);
        report("Single", single, total);
        report("Multi", total - single, total);
        final int ok = counter.getSet(FitType.OK);
        report("OK", ok, total);
        report("Fail", total - ok, total);
        final int multi = total - single;
        report("FailSingle", counter.getUnset(FitType.OK | FitType.MULTI), single);
        report("FailMulti", counter.get(FitType.MULTI, FitType.OK), multi);
        report("FitSingle", counter.get(FitType.OK, FitType.MULTI), ok);
        report("FitSingleSingle", counter.get(FitType.OK, FitType.MULTI | FitType.DOUBLET_OK), ok);
        report("FitSingleDoublet", counter.get(FitType.DOUBLET_OK, FitType.MULTI), ok);
        report("FitMulti", counter.getSet(FitType.OK | FitType.MULTI), ok);
        report("FitMultiSingle", counter.getSet(FitType.MULTI_OK), ok);
        report("FitMultiDoublet", counter.getSet(FitType.MULTI_DOUBLET_OK), ok);
        report("FailMultiFitSingle", counter.get(FitType.OK | FitType.MULTI, FitType.MULTI_OK | FitType.MULTI_DOUBLET_OK | FitType.DOUBLET_OK), ok);
        report("FailMultiFitDoublet", counter.get(FitType.OK | FitType.MULTI | FitType.DOUBLET_OK, FitType.MULTI_OK | FitType.MULTI_DOUBLET_OK), ok);
    }
    threads.clear();
}
Also used : ConcurrentRuntimeException(org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)

Aggregations

ConcurrentRuntimeException (org.apache.commons.lang3.concurrent.ConcurrentRuntimeException)16 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)10 LinkedList (java.util.LinkedList)9 Ticker (uk.ac.sussex.gdsc.core.logging.Ticker)9 ImageStack (ij.ImageStack)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ArrayList (java.util.ArrayList)5 PeakResultPoint (uk.ac.sussex.gdsc.smlm.results.PeakResultPoint)5 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)4 List (java.util.List)4 BasePoint (uk.ac.sussex.gdsc.core.match.BasePoint)4 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)3 FitWorker (uk.ac.sussex.gdsc.smlm.engine.FitWorker)3 Rectangle (java.awt.Rectangle)2 Nullable (uk.ac.sussex.gdsc.core.annotation.Nullable)2 HistogramPlotBuilder (uk.ac.sussex.gdsc.core.ij.HistogramPlot.HistogramPlotBuilder)2 WindowOrganiser (uk.ac.sussex.gdsc.core.ij.plugin.WindowOrganiser)2 Coordinate (uk.ac.sussex.gdsc.core.match.Coordinate)2 SettingsList (uk.ac.sussex.gdsc.core.utils.SettingsList)2 Statistics (uk.ac.sussex.gdsc.core.utils.Statistics)2