[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Threading question

Guys

Quick threading question...  if I want to run the bottom code continously... and check at a certain time to do something would this be the best way?    Should I extend thread instead?  I have to limit the amount of threads I create too, which is why I am not doing a TimerTask

public class MyServer implements Runnable
{

                            
    public void run()                      
    {                              
          while (true){
            System.out.println("entering server thread loop");
            try {
                  
           
            if (some time check here)
                       //times are the same or the time has passed important time
                         {
                            //do code
                         }
                 else
                   {
                         Thread.sleep((long)(Math.random() * 1000));

                        }
               
            } catch (InterruptedException e) {}
                    }    
          
          
          
          
    }

    public static void main(String[] args) {
        Thread mythread = new Thread(new MyServer(), "MyServer");
        mythread.start();
    }
      
}

thanks
ryno71
0
ryno71
Asked:
ryno71
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
CEHJCommented:
0
 
ryno71Author Commented:
Why do you think the sleep would eat up CPU?
0
 
objectsCommented:
>                       Thread.sleep((long)(Math.random() * 1000));

If you want to check at a certain time you should not use a random sleep.
Instead sleep until the time you need to do the check.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ryno71Author Commented:
oops,  yea I plan on getting the sleep time from a config file...  not random :)  thanks

If I can't make aditional threads (timer thread or timertask)... this would work fine right?

I had planned on using a Calendar object (two) to make my time comparisons

thanks
ryno71
0
 
objectsCommented:
yes that should work fine.

TimerTask's are all run on a single thread though (assuming you only use a single Timer)
0
 
CEHJCommented:
>>Why do you think the sleep would eat up CPU?

It will potentially eat up more CPU if the thread is in a busy wait (sleep) state, yes. And using a Timer will not use any more threads than you would use doing it yourself.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Timer is designed for such usages, so better use something already available :)
0
 
ryno71Author Commented:
If I am allowed to I will .. you know how it is :)!
0
 
CEHJCommented:
:-)
0
 
ryno71Author Commented:
thanks for the help guys.

ryno71
0

Featured Post

Technology Partners: 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!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now