Click to See Complete Forum and Search --> : threading issues
pouncer
July 18th, 2008, 06:48 PM
public class Test {
public static void main(String[] args) {
GamePlayer game = new GamePlayer();
System.out.println(game.doSomeMove());
}
}
class GamePlayer {
int doSomeMove() {
// Cause some timeout on purpose.. 7 seconds!
Long stoptime = 7000L;
try {
Thread.sleep(stoptime);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 5;
}
}
class ThreadTesting implements Runnable {
public void start() {
}
public void stop() {
}
public void run() {
try {
}
catch (Exception e) {
}
}
}
I'm not too sure on how to do this.
as you see i deliberately make the doSomeMove() return the int after 7 seconds. i want to somehow run the doSomeMove() method in a thread and check it for timeout issues. If the method takes more than 5 seconds to respond, i want to stop the thread and print "player move took too long to repsond" otherwise i return the move (the int) as normal. can anyone help me with this
Norm
July 18th, 2008, 09:58 PM
Put the call to doSomeMove in a Thread and have the main/calling routine join(<timeout>) that thread. Have some kind of flag set in run() when doSomeMore returns that the main code can test to see if it was a timeout or a completion. Use interrupt on the thread. Of course, the thread can catch that and ignore it, so you'll need code in the run() method of the thread to ignore anything returned by doSomeMove.
pouncer
July 19th, 2008, 05:27 AM
Thanks for the reply Norm. This is what I've worked up:
public class Test {
public static void main(String[] args) throws InterruptedException {
ThreadTesting tt = new ThreadTesting();
Thread t = new Thread(tt);
long patience = 2000;
long startTime = System.currentTimeMillis();
t.start();
while (t.isAlive()) {
// Keep checking if threads time goes over 'patience' seconds..
if (((System.currentTimeMillis() - startTime) > patience) && t.isAlive()) {
System.out.println("timeout......");
t.stop();
break;
}
if (tt.getValue() != null) {
long total_time = System.currentTimeMillis() - startTime;
System.out.println("Got a return value: " + tt.getValue() + " in " + total_time + " ms.");
t.stop();
break;
}
}
}
}
class GamePlayer {
int doSomeMove() {
// Cause some timeout on purpose..
try {
Thread.sleep(1000);
}
catch (InterruptedException e) { }
return 5;
}
}
class ThreadTesting implements Runnable {
Integer returnedValue;
public void start() {
}
public void stop() {
}
public void run() {
try {
GamePlayer gp = new GamePlayer();
returnedValue = gp.doSomeMove();
}
catch (Exception e) {
}
}
public Integer getValue() {
return returnedValue;
}
}
Any ways you would improve the code?
Londbrok
July 19th, 2008, 09:59 AM
If you were actually using Threads... No not use stop(), EVER.
Treat Threads as any other code. It will execute what ever is inside the run() method, once done with it, the Thread will close. Stop, pause, resume etc. are deprecated, and make more turns on the road, that needs to be straight forward.
And again, if you want something executed every 2 seconds... why do it that way. Basically you allready have the timeout with sleep(...) within the thread itself, why not be happy with that? The persistent if clauses etc. just burn up needless steam.
Also you should let the Thread do something meaningfull (would be depressing to see a Thread with that great a function), having two classes (GamePlayer and the test ) just for the fun of returning a fixed number, that is a bit overkill. Why does the Thread not have an instance of (GameBoard or something) that needs the return value of doSomeMove? Your code is hacked into so small chunks that on their own any of the have absolutely no meaning. What is the straightest, still compact road to whatever you want to accomplish?
If you are attempting to create a system that plays a simulated game, have two Gameplayer threads, sharing the same GameBoard instance, making moves to it, untill GameBoard declares the game over.
And remember.... Thread != Runnable. Just using runnables does not mean that any new Thread is invoked (and writing empty start / stop does not make it more thread like.). If you looked at the Runnables Api's it would declare that it is an interface, implemented by anything that was to executed by a Thread... only then you would have an actual another Thread.
While you are at it, executors are also worth checking out.
Norm
July 19th, 2008, 10:07 AM
My idea was to use join(), not sleep() and loops:
public class TimeoutTest {
static class Worker {
void doit() { // simulate doing something
try{Thread.sleep(3000);}catch(Exception x){}
}
}
static Worker wkr = new Worker();
static boolean done = false;
public static void main(String[] args) {
// Start a method in on its thread and check if its done in x seconds
System.out.println("Starting at " + System.currentTimeMillis());
Thread t = new Thread(new Runnable() {
public void run() {
wkr.doit();
done = true;
System.out.println("doit done at " + System.currentTimeMillis());
}
});
t.start();
// What ends first? doit() or 1 sec wait?
try {t.join(1000);}catch(Exception e){}
if(!done) {
System.out.println("doit NOT done at " + System.currentTimeMillis());
t.interrupt();
}
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.