2

i just started to learn programming (2 weeks ago), and i am trying to make a bot for a game. In the main class of the bot, there are 3 methods that needs to be returned within 2second, or it will return null. I want to avoid returning null and return what it has calculate during 2sec instead.

public ArrayList<PlaceArmiesMove> getPlaceArmiesMoves(BotState state, Long timeOut){

ArrayList<PlaceArmiesMove> placeArmiesMoves = new ArrayList<PlaceArmiesMove>();

// caculations filling the ArrayList

return placeArmiesMoves;
}

what i want to do is after 2 second, returning placeArmiesMoves, wether the method finished running or not. I have read about guava SimpleTimeLimiter and callWithTimeout() but i am totally lost about how to use it (i read something about multithreading but i just don't understand what this is) i would be incredibly grateful if someone could help me! thanks

125ch209
  • 35
  • 1
  • 6
  • 3
    2 weeks of programming and you try to build app with threads? – ruhungry Mar 31 '14 at 15:11
  • Multithreading is broken to only one sentence the ability to run multiple jobs at the same time. For example running a search for a file while you can still do something withing the application like renaming a file. – LostKatana Mar 31 '14 at 15:14
  • +1 for making a game at week 2 . good luck . nevered used times . guess some events – Srinath Ganesh Mar 31 '14 at 15:17
  • http://stackoverflow.com/questions/5710911/i-want-to-implement-a-timer-for-a-game-java – Srinath Ganesh Mar 31 '14 at 15:35
  • i only started learning Java so that i could take part in an AI competition for game i like, however i am familiar with the "logic" of programming, since i used Matlab and Mathematica in the past, so i guess i'm not completely new to programming. I have already have a Bot that is currently in a really good position in the competition, but i still lose some games due to my bot crashing because it doesn't return the moves in due time (i think this is why it crashes). I don't know what is a thread in java but i'll do some research since it seems to be the solution to my problem. Anyway, thanks! – 125ch209 Mar 31 '14 at 20:19

2 Answers2

1

Given a function like getPlaceArmiesMove, there are several techniques you might use to bound its execution time.

Trust the function to keep track of time itself

If the function runs a loop, it can check on every iteration whether the time has expired.

long startTime = System.currentTimeMillis()
for (;;) {
    // do some work
    long elapsed = System.currentTimeMillis() - startTime;
    if (elapsed >= timeOut) {
        break;
    }
}

This technique is simple, but there is no guarantee it will complete before the timeout; it depends on the function and how granular you can make the work (of course, if it's too granular, you'll be spending more time testing if the timeout has expired than actually doing work).

Run the function in a thread, and ask it to stop

I'm not familiar with Guava, but this seems to be what SimpleTimeLimiter is doing. In Java, it isn't generally possible to forcibly stop a thread, though it is possible to ignore the thread after a timeout (the function will run to completion, but you've already used its partial result, and ignore the complete result that comes in too late). Guava says that it interrupts the thread if it has not returned before the timeout. This works only if your function is testing to see if it has been interrupted, much like the "trust your function" technique.

See this answer for an example on how to test if your thread has been interrupted. Note that some Java methods (like Thread.sleep) may throw InterruptedException if the thread is interrupted.

In the end, sprinkling checks for isInterrupted() all over your function won't be much different than sprinkling manual checks for the timeout. So running in a thread, you still must trust your function, but there may be nicer helpers available for that sort of thing (e.g. Guava).

Run the function in a separate process, and kill it

An example of how to do this is left as an exercise, but if you run your function in a separate process (or a thread in languages that support forcibly stopping threads, e.g. Erlang, Ruby, others), then you can use the operating system facilities to kill the process if it does not complete after a timeout.

Having that process return a partial result will be challenging. It could periodically send "work-in-progress" to the calling process over a pipe, or periodically save work to a file.

Community
  • 1
  • 1
Patrick
  • 5,714
  • 4
  • 31
  • 36
  • The getPlaceArmiesMove method actually has more than 600 lines of Fors and Ifs,so i don't think the "trust the function" will help me, i'll try the thread thingy. If you have a good tutorial or guide about threads in java, i'll take it!Thanks a lot for your answer – 125ch209 Mar 31 '14 at 20:28
  • I added a link showing how to check if your thread has been interrupted. You still must trust your function though. – Patrick Mar 31 '14 at 23:57
0

Use Java's Timer package , however this will require you to understand concepts such as threads and method overriding. Nevertheless, if this is what you require, the answer is quite similar to this question How to set a timer in java

Community
  • 1
  • 1
numX
  • 830
  • 7
  • 24