Link to home
Start Free TrialLog in
Avatar of romram
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
Avatar of ksivananth
ksivananth
Flag of United States of America image

whats the problem now?
Avatar of romram
romram

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.
can u post the code?
on the other hand, you can try Timer and TimerTask...

http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html
Avatar of romram

ASKER

Here is the run method
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;
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of romram
romram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial