Skip to content

Commit

Permalink
Merge pull request #10 from Evegen55/use_executor_servise
Browse files Browse the repository at this point in the history
Executor servise
  • Loading branch information
klevis authored Dec 12, 2017
2 parents 1cb859c + 235101c commit d2cbb68
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
17 changes: 6 additions & 11 deletions src/main/java/ramo/klevis/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Created by klevis.ramo on 11/24/2017.
Expand All @@ -18,6 +20,8 @@ public class Run {

private final static Logger LOGGER = LoggerFactory.getLogger(Run.class);

public static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();

private static JFrame mainFrame = new JFrame();

public static void main(String[] args) throws Exception {
Expand All @@ -27,17 +31,8 @@ public static void main(String[] args) throws Exception {
setHadoopHomeEnvironmentVariable();
ProgressBar progressBar = new ProgressBar(mainFrame, true);
progressBar.showProgressBar("Collecting data this make take several seconds!");
new Thread(() -> {
try {
new UI().initUI();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
progressBar.setVisible(false);
mainFrame.dispose();
}
}).start();

UI ui = new UI();
EXECUTOR_SERVICE.submit(ui::initUI);
}


Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ramo/klevis/nn/NeuralNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class NeuralNetwork {
public void init() {
initSparkSession();
if (model == null) {
LOGGER.info("Loading the Neural Network from saved model ... ");
model = MultilayerPerceptronClassificationModel.load("ModelWith60000");
LOGGER.info("Loading from saved model is done");
}
}

Expand Down Expand Up @@ -65,7 +67,7 @@ private void evalOnTest(Dataset<Row> test) {
MulticlassClassificationEvaluator evaluator = new MulticlassClassificationEvaluator()
.setMetricName("accuracy");

System.out.println("Test set accuracy = " + evaluator.evaluate(predictionAndLabels));
LOGGER.info("Test set accuracy = " + evaluator.evaluate(predictionAndLabels));
}

private void initSparkSession() {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/ramo/klevis/ui/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.File;
import java.io.IOException;

import static ramo.klevis.Run.EXECUTOR_SERVICE;

public class UI {

private final static Logger LOGGER = LoggerFactory.getLogger(UI.class);
Expand Down Expand Up @@ -116,18 +118,19 @@ private void addTopPanel() {
if (i == JOptionPane.OK_OPTION) {
ProgressBar progressBar = new ProgressBar(mainFrame);
SwingUtilities.invokeLater(() -> progressBar.showProgressBar("Training this may take one or two minutes..."));
Runnable runnable = () -> {
EXECUTOR_SERVICE.submit(() -> {
try {
LOGGER.info("Start of train Neural Network");
neuralNetwork.train((Integer) trainField.getValue(), (Integer) testField.getValue());
LOGGER.info("End of train Neural Network");
} catch (IOException ex) {
LOGGER.error("An error when train Neural Network");
throw new RuntimeException(ex);
} finally {
progressBar.setVisible(false);
}
};
new Thread(runnable).start();
});
}

});

topPanel.add(train);
Expand Down

0 comments on commit d2cbb68

Please sign in to comment.