how to keep track of ThreadUtil.bgRun thread and stop them eventually?

Home Forums Development how to keep track of ThreadUtil.bgRun thread and stop them eventually?

  • Frederic Marion-Poll

    This is again a nerd’s question, sorry for that.

    In order to make a plugin more responsive and allow the user to do something while the computer is loading a bunch of files, I launch different reading in separate threads. However, the user may want to switch to another set of data without waiting that the program has loaded all dependent files. How can I access these different threads and stop them?

    Below is a snippet of the corresponding program. The idea is that a user opens a sequence (a stack of jpg files) and the routine below will open 4 types of measures done on these files (capillaries, kymographs, cages, measures). Reading kymographs and cages is quite lengthy, so that the corresponding routines are called within a ThreadUtil.bgRun thread.

     

    How can we stop these threads before completion?

    fred

     

    private void loadPreviousMeasures(boolean loadCapillaries, boolean loadKymographs, boolean loadCages, boolean loadMeasures) {
    vSequence.removeAllROI();
    if (loadCapillaries) {
    if( !capillariesPane.loadDefaultCapillaries())
    return;
    sequencePane.browseTab.setBrowseItems(this.vSequence);
    capillariesPane.propertiesTab.visibleCheckBox.setSelected(true);
    }

    if (loadKymographs) {
    ThreadUtil.bgRun( new Runnable() { @Override public void run() {
    if ( !capillariesPane.fileTab.loadDefaultKymos()) {
    return;
    }
    if (loadMeasures) {
    kymographsPane.fileTab.measuresFileOpen();
    if (sequencePane.openTab.graphsCheckBox.isSelected())
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    kymographsPane.graphsTab.xyDisplayGraphs();
    }
    });
    }
    }});
    }

    if (loadCages) {
    ThreadUtil.bgRun( new Runnable() { @Override public void run() {
    movePane.loadDefaultCages();
    movePane.graphicsTab.moveCheckbox.setEnabled(true);
    movePane.graphicsTab.displayResultsButton.setEnabled(true);
    if (vSequence.cages != null && vSequence.cages.flyPositionsList.size() > 0) {
    double threshold = vSequence.cages.flyPositionsList.get(0).threshold;
    movePane.graphicsTab.aliveThresholdSpinner.setValue(threshold);
    }
    }});
    }
    }

    Stephane Dallongeville

    Hi Fred,

    ThreadUtil.bgRun(..) doesn’t let you to interrupt thread and in fact you cannot really interrupt a thread in java. Instead you have to take care of that yourself by providing a flag in your executor code to interrupt the process :

    // need to interrupt process ? –> return now
    if (isInterrupted) return;

    Note that Thread object does have an interrupt properties you can check for, but you cannot directly change it for the BG runner of Icy (you need to do your own thread in this case). But of course you can always have your own ‘interrupt’ flag in your Runner class 🙂

    Hope that help.

    – Stephane

    Frederic Marion-Poll

    oh, thank you!it works indeed.

    I was wondering if there was a way to call ThreadUtil.shutdown(i), as when ThreadUtil.shutdown(), the program iterates through 2 lists of processes (instanceProcessors and bgInstanceProcessors), which meant to me that somehow, we could get the “id” of  any new thread created, and thus kill it later…

    fred

     

    Stephane Dallongeville

    InstanceProcessors (and bgInstanceProcessors) has a fixed number of Thread (workers) which are always actives and which can execute an unlimited number of tasks. You cannot stop a task by its Id, at best you can try to interrupt the thread which is currently executing the task but still the task need to check the interrupt state of the thread, and then we need to restore the thread execution so it could execute future submitted tasks.

    – Stephane

Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘Development’ is closed to new topics and replies.