Solved

Thread.sleep with a switch statment

Posted on 2009-04-12
6
388 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:romram
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 24128238
whats the problem now?
0
 

Author Comment

by:romram
ID: 24128256
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 24128325
can u post the code?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 26

Expert Comment

by:ksivananth
ID: 24128327
on the other hand, you can try Timer and TimerTask...

http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html
0
 

Author Comment

by:romram
ID: 24128343
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
 

Accepted Solution

by:
romram earned 0 total points
ID: 24128421
I used while(!Thread.interrupted()) and it is working...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question