Solved

Thread.sleep with a switch statment

Posted on 2009-04-12
6
383 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now