Skip to content

Commit

Permalink
Added async move method too Util
Browse files Browse the repository at this point in the history
  • Loading branch information
KarelPeeters committed Feb 12, 2017
1 parent 7557194 commit 47a7f3e
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/main/java/com/flaghacker/uttt/common/Util.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.flaghacker.uttt.common;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
Expand All @@ -12,8 +15,6 @@ public class Util
private static Random random = new Random();
private static int[] seeds = {};

private static ScheduledExecutorService exec;

public static Random loggedRandom()
{
int seed;
Expand All @@ -32,12 +33,14 @@ public static Random loggedRandom()
return new Random(seed);
}

private static ScheduledExecutorService timeOutService;

public static Coord moveBotWithTimeOut(final Bot bot, Board board, long time)
{
checkAndInitExecutor();

final boolean[] runTimeUp = {true};
exec.schedule(new Runnable()
timeOutService.schedule(new Runnable()
{
@Override
public void run()
Expand All @@ -54,12 +57,27 @@ public void run()
return move;
}

public static Future<Coord> moveBotWithTimeOutAsync(ExecutorService executor,
final Bot bot, final Board board, final long time)
{
checkAndInitExecutor();

return executor.submit(new Callable<Coord>()
{
@Override
public Coord call() throws Exception
{
return moveBotWithTimeOut(bot, board, time);
}
});
}

private static void checkAndInitExecutor()
{
if (exec != null)
if (timeOutService != null)
return;

exec = new ScheduledThreadPoolExecutor(1, new ThreadFactory()
timeOutService = new ScheduledThreadPoolExecutor(1, new ThreadFactory()
{
@Override
public Thread newThread(Runnable runnable)
Expand Down

0 comments on commit 47a7f3e

Please sign in to comment.