Solved

Thread.sleep with a switch statment

Posted on 2009-04-12
6
407 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

623 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