mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-27 00:45:23 -04:00
extract authentication to class, map to user thread
This commit is contained in:
parent
f788778f3c
commit
e3cdad4299
27 changed files with 914 additions and 695 deletions
|
@ -18,10 +18,17 @@
|
|||
package io.bitsquare.common;
|
||||
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class UserThread {
|
||||
private static final Logger log = LoggerFactory.getLogger(UserThread.class);
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return executor;
|
||||
|
@ -41,4 +48,34 @@ public class UserThread {
|
|||
public static void execute(Runnable command) {
|
||||
UserThread.executor.execute(command);
|
||||
}
|
||||
|
||||
|
||||
public static Timer runAfterRandomDelay(Runnable runnable, long minDelayInSec, long maxDelayInSec) {
|
||||
return UserThread.runAfterRandomDelay(runnable, minDelayInSec, maxDelayInSec, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public static Timer runAfterRandomDelay(Runnable runnable, long minDelay, long maxDelay, TimeUnit timeUnit) {
|
||||
return UserThread.runAfter(runnable, new Random().nextInt((int) (maxDelay - minDelay)) + minDelay, timeUnit);
|
||||
}
|
||||
|
||||
public static Timer runAfter(Runnable runnable, long delayInSec) {
|
||||
return UserThread.runAfter(runnable, delayInSec, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public static Timer runAfter(Runnable runnable, long delay, TimeUnit timeUnit) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
|
||||
try {
|
||||
UserThread.execute(() -> runnable.run());
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
log.error("Executing timerTask failed. " + t.getMessage());
|
||||
}
|
||||
}
|
||||
}, timeUnit.toMillis(delay));
|
||||
return timer;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,9 +35,6 @@ import java.net.URI;
|
|||
import java.net.URISyntaxException;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
@ -83,36 +80,6 @@ public class Utilities {
|
|||
return threadPoolExecutor;
|
||||
}
|
||||
|
||||
public static Timer runTimerTaskWithRandomDelay(Runnable runnable, long minDelay, long maxDelay) {
|
||||
return runTimerTaskWithRandomDelay(runnable, minDelay, maxDelay, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public static Timer runTimerTaskWithRandomDelay(Runnable runnable, long minDelay, long maxDelay, TimeUnit timeUnit) {
|
||||
return runTimerTask(runnable, new Random().nextInt((int) (maxDelay - minDelay)) + minDelay, timeUnit);
|
||||
}
|
||||
|
||||
public static Timer runTimerTask(Runnable runnable, long delay) {
|
||||
return runTimerTask(runnable, delay, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public static Timer runTimerTask(Runnable runnable, long delay, TimeUnit timeUnit) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
|
||||
try {
|
||||
runnable.run();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
log.error("Executing timerTask failed. " + t.getMessage());
|
||||
}
|
||||
}
|
||||
}, timeUnit.convert(delay, timeUnit));
|
||||
return timer;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isUnix() {
|
||||
return isOSX() || isLinux() || getOSName().contains("freebsd");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue