Skip to content

Commit

Permalink
[hyperspace-core-gui2] improvements in task handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lithom committed Jul 14, 2023
1 parent 69ee493 commit 0be218c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.*;

public class RealTimeExpandingSearchResultModel {

Expand Down Expand Up @@ -58,6 +55,8 @@ private RealTimeExpandingSearchResultModel getThis() {
return this;
}

private final List<Future> expansionTasks = Collections.synchronizedList(new ArrayList<>());

private void processResultsChanged() {
// we may end up here from edt.. therefore do this
// async..
Expand All @@ -83,6 +82,7 @@ public void run() {
if(moleculeOrder.size()>maxExpandedHits) {return;}
List<String> processed = new ArrayList<>();
for(SynthonAssembler.ExpandedCombinatorialHit xi : exp_hits) {
if(Thread.interrupted()) {break;}
String processed_idcode = processResultStructure(xi.assembled_idcode);
assembledMolecules.put(processed_idcode,fchi);
processed.add(processed_idcode);
Expand Down Expand Up @@ -115,7 +115,10 @@ public void run() {
if(getThis().expansionThreadPool==null || getThis().expansionThreadPool.isShutdown() ) {
getThis().restartThreadpool();
}
expansionThreadPool.submit(ri);
Future fExp = expansionThreadPool.submit(ri);
synchronized(expansionTasks) {
expansionTasks.add(fExp);
}
}
//ri.setPriority(Thread.MIN_PRIORITY);
//ri.start();
Expand All @@ -132,6 +135,17 @@ public void run() {
private ThreadPoolExecutor expansionThreadPool;

private void initExpansionThreadPool() {
if(!expansionTasks.isEmpty()) {
List<Future> allTasks = new ArrayList<>();
synchronized(expansionTasks) {
allTasks = new ArrayList<>(expansionTasks);
}
for(Future fi : allTasks) {
fi.cancel(true);
expansionTasks.remove(fi);
}
}

expansionThreadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);

// Set the lowest priority for each thread
Expand Down Expand Up @@ -277,6 +291,13 @@ public Object getValueAt(int rowIndex, int columnIndex) {
}

public void shutdownThreadpool() {
if(!expansionTasks.isEmpty()) {
List<Future> allTasks = new ArrayList<>(expansionTasks);
for(Future fi : allTasks) {
fi.cancel(true);
expansionTasks.remove(fi);
}
}
this.expansionThreadPool.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void run() {
});
}
this.initMouseContextMenu();
SwingUtilities.updateComponentTreeUI(this);
}

public JPanel getPanelTopRight() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ public static List<ExpandedCombinatorialHit> expandCombinatorialHit(SynthonSpace
current_assemblies.add(new ArrayList<>());

for(int zi=0;zi<frag_sets.size();zi++) {
if(Thread.interrupted()) {return new ArrayList<>();}
List<List<SynthonSpace.FragId>> next_assemblies = new ArrayList<>();
for( List<SynthonSpace.FragId> ca : current_assemblies) {
if(next_assemblies.size()>=max_expanded) {break;}
Expand All @@ -324,6 +325,8 @@ public static List<ExpandedCombinatorialHit> expandCombinatorialHit(SynthonSpace
List<ExpandedCombinatorialHit> results = new ArrayList<>();
IDCodeParser icp = new IDCodeParser();
for(List<SynthonSpace.FragId> api : current_assemblies) {
if(Thread.interrupted()) {return results;}

List<StereoMolecule> parts = new ArrayList<>();
List<String> parts_ids = new ArrayList<>();
for( SynthonSpace.FragId fid : api ) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<!--<maven.compiler.target>1.8</maven.compiler.target>-->
<!--<maven.compiler.release>8</maven.compiler.release>-->
<!--<maven.compiler.release>8</maven.compiler.release>-->
<revision>2.1.28-SNAPSHOT</revision>
<revision>2.1.35-SNAPSHOT</revision>
<!--<maven.compiler.source>${java.version}</maven.compiler.source>-->
<!--<maven.compiler.target>${java.version}</maven.compiler.target>-->
</properties>
Expand Down

0 comments on commit 0be218c

Please sign in to comment.