problem in implementing a timeout with Java
hi all
i wrote the following dummy class for implementing a timeout. Since in real case situations i cannot afford to create a new thread all the time, i am reusing the same one.
The code works as follows:the proceed() method check the currentTime and calculates what will be the endTime.
then the TimerThread is started, and meanwhile the class is looping in a while loop.
that while loop continues as long as two conditions are satisfied:
- the currentTime is not equal to the endTime: if that condition is not satisified, that means that we are in a timeout situation
- the TimerThread is still alive: if this condition is not satisfied that means that the job was finished in time (since at the end of run() the thread exits...
now, since i have to simulate a timeout, i used a variable called blocked that is incremented all the time. when blocked mod 3 == 0 then i make the TimerThread sleep so that a timeout is simulated.
what is funny is that:
- while the thread is sleeping, the caller class is still in the loop, and the currentTime is NOT incremented (as u will see on the screen by the printouts)
so at the end the timeout will never happens, and i cannot figure out why: i am NOT using any shared variables that can cause some sort of deadlock situation ..
here is my code, i hope that someone can find out what's wrong. i am suspected that it has to do with the fact that i am reusing the same thread all the time (since when i create a new one all the time it works fine)
import java.util.*;
import java.io.*;
public class TestTimer {
private Object proceedLock;
private boolean okToProceed;
private TimerThread timer;
private boolean started = false;
private int blocked = 1;
private boolean isStopped = false;
private boolean isCompleted = false;
public TestTimer() {
print("In TestTimer..");
timer = new TimerThread();
}
class TimerThread extends Thread {
private Thread runThread;
public boolean isCompleted = false;
public void doWork() {
run();
}
public void run() {
//System.err.println("WE ARE IN RUN METHODo: " + blocked);
runThread = Thread.currentThread();
try {
if((blocked % 3) == 0)
sleep(10000);
else {
isCompleted=true;
}
} catch(InterruptedException e) {}
}
public void stopThread() {
System.err.println("stopping the thread..");
if ( runThread != null)
runThread.interrupt();
}
}
public void proceed() throws Exception {
long startTime = System.currentTimeMillis();
long currentTime = startTime;
long endTime = startTime + 1000;
long testTime=0;
System.err.println("AT PROCEED: startTime is: " + startTime + " and endTime is: " + endTime);
if(timer.isAlive()) {
System.err.println("BEFORE EVERYTHING TIMER IS ALIVE!!");
}
if(!started) {
timer.start();
started=true;
} else {
timer.run();
}
while((currentTime < endTime) && (timer.isAlive())) {
currentTime = System.currentTimeMillis();
}
System.err.println("testTime: " + testTime);
if(timer.isAlive()) {
// this means that we are out because time has expired...
System.err.println("TIME HAS RUN OUT!");
timer.stop();
throw new Exception("there was a timeout..");
// stopping the thread..it's running for nothing..
}else {
System.err.println("we did it in time...");
System.err.println("currentTime: " + currentTime + "\t" + " endTime: " + endTime);
// we don't need to stop the thread because
// it's already over
}
blocked++;
}
private static void print(String msg) {
String name = Thread.currentThread().getName();
System.err.println(name + ": " + msg);
}
public static void main(String[] args) throws Exception {
final TestTimer timer = new TestTimer();
for(int i=0; i<3; i++) {
timer.proceed();
}
}
}
please help me soon
thanx in advance and regards
marco

