romram
asked on
Thread.sleep with a switch statment
I am having a runnable class, where in the run() method I have a switch statement.
In each case of the switch statement I want to invoke a function to generate some data then call Thread.sleep(milliseconds) ;
However the switch is visited only once.
//I want to implement my thread as a timer
In each case of the switch statement I want to invoke a function to generate some data then call Thread.sleep(milliseconds)
However the switch is visited only once.
//I want to implement my thread as a timer
whats the problem now?
ASKER
The thread is not working as a timer.
I thought that after the sleeping time for the thread is going to finish the switch is going to be visited again and the function will be called again but this is not happening.
I thought that after the sleeping time for the thread is going to finish the switch is going to be visited again and the function will be called again but this is not happening.
can u post the code?
on the other hand, you can try Timer and TimerTask...
http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html
http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html
ASKER
Here is the run method
the print statement is only printed once..
the print statement is only printed once..
public void run(){
switch (model){
case RANDOMWALK:
generateRandomWalkStream();
break;
case MYDATASET1:
generateMyDataset1();
break;
case MYDATASET2:
try{
generateMyDataset2();
System.out.println("I am here");
Thread.sleep(dataArrivalRate);
}
catch(InterruptedException ex){}
break;
default:
generateRandomWalkStream();
break;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.