Skip to content

Commit

Permalink
Added ThreadBound
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxJason committed Dec 11, 2024
1 parent d1ab3a1 commit c5a569c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ public CloudChain() {
System.out.println(ConsoleColor.apply(PREFIX + "&eInitialize Chain"));
}

public void initialize() {

}

public String getSomething() {
return "";
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package eu.smoothcloud.node;

import eu.smoothcloud.chain.CloudChain;
import eu.smoothcloud.node.configuration.LaunchConfiguration;
import eu.smoothcloud.node.console.Console;
import eu.smoothcloud.node.util.ThreadBound;
import eu.smoothcloud.node.util.ThreadManager;

import java.util.concurrent.Future;

public class SmoothCloudNode {

public static void main(String[] args) {
Expand All @@ -12,12 +16,27 @@ public static void main(String[] args) {

private LaunchConfiguration launchConfiguration;
private Console console;
private int threads = Runtime.getRuntime().availableProcessors();

private CloudChain chain;

public SmoothCloudNode() {
ThreadManager manager = new ThreadManager(12);
System.out.println("DEBUG: Before console.start()");
ThreadManager manager = new ThreadManager(launchConfiguration.getThreads() > 0 ? launchConfiguration.getThreads() : threads);
manager.startTask("Console", this::startConsole);
print(console);

ThreadBound<CloudChain> cloudChain = new ThreadBound<>(manager, "CloudChain", new CloudChain());
cloudChain.runOnThread(CloudChain::initialize);
Future<String> result = cloudChain.callOnThread(CloudChain::getSomething);
try {
System.out.println("Result: " + result.get());
} catch (Exception e) {
e.fillInStackTrace();
}
}

private void startCloudChain() {
this.chain = new CloudChain();
}

private void startConsole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@Getter
@AllArgsConstructor
public abstract class LaunchConfiguration implements JsonSerializable {

private String language;
private String host;
private int port;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package eu.smoothcloud.node.util;

import java.util.concurrent.Future;

public class ThreadBound<T> {

private final String taskName;
private final ThreadManager threadManager;
private final T instance;

public ThreadBound(ThreadManager manager, String taskName, T instance) {
this.taskName = taskName;
this.threadManager = manager;
this.instance = instance;
}

public <R> Future<R> callOnThread(CallableInstance<T, R> callable) {
return threadManager.submitTask(taskName, () -> callable.call(instance));
}

public void runOnThread(ConsumerInstance<T> consumer) {
threadManager.startTask(taskName, () -> consumer.accept(instance));
}

public T getInstance() {
return instance;
}

@FunctionalInterface
public interface CallableInstance<T, R> {
R call(T instance);
}

@FunctionalInterface
public interface ConsumerInstance<T> {
void accept(T instance);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import eu.smoothcloud.node.console.ConsoleColor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.*;

public class ThreadManager {

Expand All @@ -30,6 +27,17 @@ public void startTask(String taskName, Runnable task) {
System.out.println(ConsoleColor.apply(PREFIX + "&eTask &b" + taskName + " &estarted."));
}

public <T> Future<T> submitTask(String taskName, Callable<T> task) {
if(taskMap.containsKey(taskName)) {
System.out.println(ConsoleColor.apply(PREFIX + "&eTask &b" + taskName + " &eis already running."));
return null;
}
Future<T> future = executorService.submit(task);
taskMap.put(taskName, future);
System.out.println(ConsoleColor.apply(PREFIX + "&eTask &b" + taskName + " &estarted."));
return future;
}

public void stopTask(String taskName) {
Future<?> future = taskMap.get(taskName);
if (future != null) {
Expand Down

0 comments on commit c5a569c

Please sign in to comment.