mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
Add timeout to prevent spending forever trying to determine an Internet connection
This commit is contained in:
@@ -45,4 +45,21 @@ public class ThreadUtil {
|
||||
public static boolean isGameThread() {
|
||||
return Thread.currentThread().getName().startsWith("Game");
|
||||
}
|
||||
|
||||
public static <T> T executeWithTimeout(Callable<T> task, int milliseconds) {
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<T> future = executor.submit(task);
|
||||
T result;
|
||||
try {
|
||||
result = future.get(milliseconds, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
catch (Exception e) { //handle timeout and other exceptions
|
||||
e.printStackTrace();
|
||||
result = null;
|
||||
}
|
||||
finally {
|
||||
future.cancel(true);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user