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
romramAsked:
Who is Participating?
 
romramConnect With a Mentor Author Commented:
I used while(!Thread.interrupted()) and it is working...
0
 
ksivananthCommented:
whats the problem now?
0
 
romramAuthor Commented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ksivananthCommented:
can u post the code?
0
 
ksivananthCommented:
on the other hand, you can try Timer and TimerTask...

http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html
0
 
romramAuthor Commented:
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

0
All Courses

From novice to tech pro — start learning today.