P2P network improvements

This commit is contained in:
Manfred Karrer 2016-02-25 14:48:00 +01:00
parent 900f89ce37
commit f0d727e345
23 changed files with 218 additions and 257 deletions

View file

@ -1,69 +0,0 @@
package io.bitsquare.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.Random;
import java.util.TimerTask;
public class DefaultJavaTimer implements Timer {
private final Logger log = LoggerFactory.getLogger(DefaultJavaTimer.class);
private java.util.Timer timer;
public DefaultJavaTimer() {
}
@Override
public Timer runLater(Duration delay, Runnable runnable) {
if (timer == null) {
timer = new java.util.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());
}
}
}, delay.toMillis());
} else {
log.warn("runLater called on an already running timer.");
}
return this;
}
@Override
public Timer runPeriodically(java.time.Duration interval, Runnable runnable) {
if (timer == null) {
timer = new java.util.Timer();
timer.scheduleAtFixedRate(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());
}
}
}, interval.toMillis(), interval.toMillis());
} else {
log.warn("runLater called on an already running timer.");
}
return this;
}
@Override
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
}

View file

@ -3,6 +3,8 @@ package io.bitsquare.common;
import java.time.Duration;
public interface Timer {
boolean STRESS_TEST = false;
Timer runLater(java.time.Duration delay, Runnable action);
Timer runPeriodically(Duration interval, Runnable runnable);

View file

@ -46,7 +46,7 @@ public class UserThread {
static {
// If not defined we use same thread as caller thread
executor = MoreExecutors.directExecutor();
timerClass = DefaultJavaTimer.class;
timerClass = FrameRateTimer.class;
}
private static Executor executor;

View file

@ -60,8 +60,8 @@ public class Utilities {
public static ListeningExecutorService getListeningExecutorService(String name,
int corePoolSize,
int maximumPoolSize,
long keepAliveTime) {
return MoreExecutors.listeningDecorator(getThreadPoolExecutor(name, corePoolSize, maximumPoolSize, keepAliveTime));
long keepAliveTimeInSec) {
return MoreExecutors.listeningDecorator(getThreadPoolExecutor(name, corePoolSize, maximumPoolSize, keepAliveTimeInSec));
}
public static ThreadPoolExecutor getThreadPoolExecutor(String name,