Use GlobalThreadPool in conjunction with CompletableFuture

This commit is contained in:
0xb00bface 2021-01-10 20:14:36 +01:00
parent 3c1e0eea96
commit 4f7060dfef
6 changed files with 24 additions and 25 deletions

View File

@ -6,6 +6,7 @@ import java.security.NoSuchAlgorithmException;
import java.time.Instant; import java.time.Instant;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import ctbrec.GlobalThreadPool;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.recorder.Recorder; import ctbrec.recorder.Recorder;
import ctbrec.ui.controls.Dialogs; import ctbrec.ui.controls.Dialogs;
@ -36,6 +37,6 @@ public class RemoveTimeLimitAction {
Dialogs.showError(source.getScene(), "Error", "Couln't remove stop date", e); Dialogs.showError(source.getScene(), "Error", "Couln't remove stop date", e);
return false; return false;
} }
}).whenComplete((r,e) -> source.setCursor(Cursor.DEFAULT)); }, GlobalThreadPool.get()).whenComplete((r,e) -> source.setCursor(Cursor.DEFAULT));
} }
} }

View File

@ -13,6 +13,7 @@ import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.GlobalThreadPool;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.SubsequentAction; import ctbrec.SubsequentAction;
import ctbrec.recorder.Recorder; import ctbrec.recorder.Recorder;
@ -82,7 +83,7 @@ public class SetStopDateAction {
} }
} }
return true; return true;
}).whenComplete((r, e) -> { }, GlobalThreadPool.get()).whenComplete((r, e) -> {
source.setCursor(Cursor.DEFAULT); source.setCursor(Cursor.DEFAULT);
if (e != null) { if (e != null) {
LOG.error("Error", e); LOG.error("Error", e);

View File

@ -9,13 +9,13 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.GlobalThreadPool;
import ctbrec.Hmac; import ctbrec.Hmac;
import ctbrec.Settings; import ctbrec.Settings;
import ctbrec.Settings.DirectoryStructure; import ctbrec.Settings.DirectoryStructure;
@ -436,7 +436,7 @@ public class SettingsTab extends Tab implements TabSelectionListener {
} }
public void saveConfig() { public void saveConfig() {
CompletableFuture.runAsync(() -> { GlobalThreadPool.submit(() -> {
try { try {
Config.getInstance().save(); Config.getInstance().save();
} catch (IOException e) { } catch (IOException e) {

View File

@ -19,6 +19,7 @@ import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.GlobalThreadPool;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.recorder.Recorder; import ctbrec.recorder.Recorder;
import ctbrec.sites.camsoda.Camsoda; import ctbrec.sites.camsoda.Camsoda;
@ -136,7 +137,7 @@ public class CamsodaShowsTab extends Tab implements TabSelectionListener {
}); });
} }
}; };
new Thread(task).start(); GlobalThreadPool.submit(task);
} }
@Override @Override
@ -180,17 +181,17 @@ public class CamsodaShowsTab extends Tab implements TabSelectionListener {
grid.add(createLabel(formatter.format(endTime), false), 1, 1); grid.add(createLabel(formatter.format(endTime), false), 1, 1);
Button record = new Button("Record Model"); Button record = new Button("Record Model");
record.setTooltip(new Tooltip(record.getText())); record.setTooltip(new Tooltip(record.getText()));
record.setOnAction((evt) -> record(model)); record.setOnAction(evt -> record(model));
grid.add(record, 1, 2); grid.add(record, 1, 2);
GridPane.setMargin(record, new Insets(10)); GridPane.setMargin(record, new Insets(10));
Button follow = new Button("Follow"); Button follow = new Button("Follow");
follow.setTooltip(new Tooltip(follow.getText())); follow.setTooltip(new Tooltip(follow.getText()));
follow.setOnAction((evt) -> follow(model)); follow.setOnAction(evt -> follow(model));
grid.add(follow, 1, 3); grid.add(follow, 1, 3);
GridPane.setMargin(follow, new Insets(10)); GridPane.setMargin(follow, new Insets(10));
Button openInBrowser = new Button("Open in Browser"); Button openInBrowser = new Button("Open in Browser");
openInBrowser.setTooltip(new Tooltip(openInBrowser.getText())); openInBrowser.setTooltip(new Tooltip(openInBrowser.getText()));
openInBrowser.setOnAction((evt) -> DesktopIntegration.open(model.getUrl())); openInBrowser.setOnAction(evt -> DesktopIntegration.open(model.getUrl()));
grid.add(openInBrowser, 1, 4); grid.add(openInBrowser, 1, 4);
GridPane.setMargin(openInBrowser, new Insets(10)); GridPane.setMargin(openInBrowser, new Insets(10));
root.setCenter(grid); root.setCenter(grid);
@ -202,7 +203,7 @@ public class CamsodaShowsTab extends Tab implements TabSelectionListener {
private void follow(Model model) { private void follow(Model model) {
setCursor(Cursor.WAIT); setCursor(Cursor.WAIT);
new Thread(() -> { GlobalThreadPool.submit(() -> {
try { try {
SiteUiFactory.getUi(model.getSite()).login(); SiteUiFactory.getUi(model.getSite()).login();
model.follow(); model.follow();
@ -210,30 +211,26 @@ public class CamsodaShowsTab extends Tab implements TabSelectionListener {
LOG.error("Couldn't follow model {}", model, e); LOG.error("Couldn't follow model {}", model, e);
showErrorDialog("Oh no!", "Couldn't follow model", e.getMessage()); showErrorDialog("Oh no!", "Couldn't follow model", e.getMessage());
} finally { } finally {
Platform.runLater(() -> { Platform.runLater(() -> setCursor(Cursor.DEFAULT));
setCursor(Cursor.DEFAULT);
});
} }
}).start(); });
} }
private void record(Model model) { private void record(Model model) {
setCursor(Cursor.WAIT); setCursor(Cursor.WAIT);
new Thread(() -> { GlobalThreadPool.submit(() -> {
try { try {
recorder.addModel(model); recorder.addModel(model);
} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | IOException e) { } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | IOException e) {
showErrorDialog("Oh no!", "Couldn't add model to the recorder", "Recorder error: " + e.getMessage()); showErrorDialog("Oh no!", "Couldn't add model to the recorder", "Recorder error: " + e.getMessage());
} finally { } finally {
Platform.runLater(() -> { Platform.runLater(() -> setCursor(Cursor.DEFAULT));
setCursor(Cursor.DEFAULT);
});
} }
}).start(); });
} }
private void loadImage(Model model, ImageView thumb) { private void loadImage(Model model, ImageView thumb) {
new Thread(() -> { GlobalThreadPool.submit(() -> {
try { try {
String url = camsoda.getBaseUrl() + "/api/v1/user/" + model.getName(); String url = camsoda.getBaseUrl() + "/api/v1/user/" + model.getName();
Request detailRequest = new Request.Builder().url(url).build(); Request detailRequest = new Request.Builder().url(url).build();
@ -270,7 +267,7 @@ public class CamsodaShowsTab extends Tab implements TabSelectionListener {
} catch (Exception e) { } catch (Exception e) {
LOG.error("Couldn't load model details", e); LOG.error("Couldn't load model details", e);
} }
}).start(); });
} }
private Node createLabel(String string, boolean bold) { private Node createLabel(String string, boolean bold) {

View File

@ -21,6 +21,7 @@ import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.GlobalThreadPool;
import ctbrec.Model; import ctbrec.Model;
import ctbrec.Model.State; import ctbrec.Model.State;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
@ -285,7 +286,7 @@ public class ThumbCell extends StackPane {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
return false; return false;
} }
}).whenComplete((result, exception) -> { }, GlobalThreadPool.get()).whenComplete((result, exception) -> {
startPreview = null; startPreview = null;
if (result.booleanValue()) { if (result.booleanValue()) {
setPreviewVisible(previewTrigger, true); setPreviewVisible(previewTrigger, true);
@ -587,7 +588,7 @@ public class ThumbCell extends StackPane {
} finally { } finally {
Platform.runLater(() -> setCursor(Cursor.DEFAULT)); Platform.runLater(() -> setCursor(Cursor.DEFAULT));
} }
}); }, GlobalThreadPool.get());
} }
void recordLater(boolean recordLater) { void recordLater(boolean recordLater) {

View File

@ -1,10 +1,9 @@
package ctbrec.ui.tabs; package ctbrec.ui.tabs;
import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.GlobalThreadPool;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.ui.CamrecApplication; import ctbrec.ui.CamrecApplication;
import ctbrec.ui.CamrecApplication.Release; import ctbrec.ui.CamrecApplication.Release;
@ -47,7 +46,7 @@ public class UpdateTab extends Tab implements TabSelectionListener {
} }
public void loadChangeLog() { public void loadChangeLog() {
CompletableFuture.runAsync(() -> { GlobalThreadPool.submit(() -> {
Request req = new Request.Builder().url("https://pastebin.com/raw/fiAPtM0s").build(); Request req = new Request.Builder().url("https://pastebin.com/raw/fiAPtM0s").build();
try (Response resp = CamrecApplication.httpClient.execute(req)) { try (Response resp = CamrecApplication.httpClient.execute(req)) {
if (resp.isSuccessful()) { if (resp.isSuccessful()) {