Restrict EssentialsTimer to 10ms execution time

This commit is contained in:
snowleo 2013-04-22 01:23:21 +03:00
parent 547e262061
commit c2eb7b05b3

View File

@ -14,6 +14,9 @@ public class EssentialsTimer implements Runnable
private final transient Set<User> onlineUsers = new HashSet<User>(); private final transient Set<User> onlineUsers = new HashSet<User>();
private transient long lastPoll = System.currentTimeMillis(); private transient long lastPoll = System.currentTimeMillis();
private final transient LinkedList<Float> history = new LinkedList<Float>(); private final transient LinkedList<Float> history = new LinkedList<Float>();
private final int skip1 = 0;
private final int skip2 = 0;
private final long maxTime = 10 * 1000000;
EssentialsTimer(final IEssentials ess) EssentialsTimer(final IEssentials ess)
{ {
@ -23,6 +26,7 @@ public class EssentialsTimer implements Runnable
@Override @Override
public void run() public void run()
{ {
final long startTime = System.nanoTime();
final long currentTime = System.currentTimeMillis(); final long currentTime = System.currentTimeMillis();
long timeSpent = (currentTime - lastPoll) / 1000; long timeSpent = (currentTime - lastPoll) / 1000;
if (timeSpent == 0) if (timeSpent == 0)
@ -39,8 +43,21 @@ public class EssentialsTimer implements Runnable
history.add(tps); history.add(tps);
} }
lastPoll = currentTime; lastPoll = currentTime;
int count = 0;
for (Player player : ess.getServer().getOnlinePlayers()) for (Player player : ess.getServer().getOnlinePlayers())
{ {
count++;
if (skip1 > 0)
{
skip1--;
continue;
}
if (count % 10 == 0) {
if (System.nanoTime() - startTime > maxTime / 2) {
skip1 = count - 1;
break;
}
}
try try
{ {
final User user = ess.getUser(player); final User user = ess.getUser(player);
@ -54,9 +71,22 @@ public class EssentialsTimer implements Runnable
} }
} }
count = 0;
final Iterator<User> iterator = onlineUsers.iterator(); final Iterator<User> iterator = onlineUsers.iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
count++;
if (skip2 > 0)
{
skip2--;
continue;
}
if (count % 10 == 0) {
if (System.nanoTime() - startTime > maxTime) {
skip2 = count - 1;
break;
}
}
final User user = iterator.next(); final User user = iterator.next();
if (user.getLastOnlineActivity() < currentTime && user.getLastOnlineActivity() > user.getLastLogout()) if (user.getLastOnlineActivity() < currentTime && user.getLastOnlineActivity() > user.getLastLogout())
{ {