Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread.sleep with a switch statment

Posted on 2009-04-12
6
Medium Priority
?
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

722 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