Solved

Thread.sleep with a switch statment

Posted on 2009-04-12
6
379 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

14 Experts available now in Live!

Get 1:1 Help Now