* Some minor changes.

This commit is contained in:
LevelX2 2016-10-26 07:50:32 +02:00
parent f0206537c1
commit 450d850ab4
2 changed files with 39 additions and 31 deletions

View file

@ -59,7 +59,7 @@ public class UserManager {
private final ConcurrentHashMap<UUID, User> users = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, User> usersByName = new ConcurrentHashMap<>();
private static final ExecutorService CALL_EXECUTOR = ThreadExecutor.getInstance().getCallExecutor();
private static final ExecutorService USER_EXECUTOR = ThreadExecutor.getInstance().getCallExecutor();
private static final UserManager INSTANCE = new UserManager();
@ -136,7 +136,7 @@ public class UserManager {
if (userId != null) {
final User user = users.get(userId);
if (user != null) {
CALL_EXECUTOR.execute(
USER_EXECUTOR.execute(
new Runnable() {
@Override
public void run() {
@ -212,7 +212,7 @@ public class UserManager {
}
public void updateUserHistory() {
CALL_EXECUTOR.execute(new Runnable() {
USER_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
for (String updatedUser : UserStatsRepository.instance.updateUserStats()) {

View file

@ -25,7 +25,6 @@
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.server.util;
import java.util.concurrent.ExecutorService;
@ -42,22 +41,28 @@ import java.util.concurrent.TimeUnit;
public class ThreadExecutor {
private static final ExecutorService callExecutor = Executors.newCachedThreadPool();
private static final ExecutorService userExecutor = Executors.newCachedThreadPool();
private static final ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads());
private static final ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(4);
private static final ScheduledExecutorService timeoutIdleExecutor = Executors.newScheduledThreadPool(4);
/**
* noxx: what the settings below do is setting the ability to keep OS threads for new games for 60 seconds
* If there is no new game created within this time period, the thread may be discarded.
* But anyway if new game is created later, new OS/java thread will be created for it
* taking MaxGameThreads limit into account.
* noxx: what the settings below do is setting the ability to keep OS
* threads for new games for 60 seconds If there is no new game created
* within this time period, the thread may be discarded. But anyway if new
* game is created later, new OS/java thread will be created for it taking
* MaxGameThreads limit into account.
*
* This all is done for performance reasons as creating new OS threads is resource consuming process.
* This all is done for performance reasons as creating new OS threads is
* resource consuming process.
*/
static {
((ThreadPoolExecutor) callExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) callExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) callExecutor).setThreadFactory(new XMageThreadFactory("CALL"));
((ThreadPoolExecutor) userExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) userExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) userExecutor).setThreadFactory(new XMageThreadFactory("USER"));
((ThreadPoolExecutor) gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS);
((ThreadPoolExecutor) gameExecutor).allowCoreThreadTimeOut(true);
((ThreadPoolExecutor) gameExecutor).setThreadFactory(new XMageThreadFactory("GAME"));
@ -75,7 +80,8 @@ public class ThreadExecutor {
return INSTANCE;
}
private ThreadExecutor() {}
private ThreadExecutor() {
}
public int getActiveThreads(ExecutorService executerService) {
if (executerService instanceof ThreadPoolExecutor) {
@ -109,10 +115,12 @@ class XMageThreadFactory implements ThreadFactory {
XMageThreadFactory(String prefix) {
this.prefix = prefix;
}
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName(prefix + " " + thread.getThreadGroup().getName() + "-" + thread.getId());
return thread;
}
}