1 2 Previous Next 15 Replies Latest reply: Jul 28, 2007 1:12 PM by 807605 RSS

    Run a thread every two seconds

    807605
      How can I run a thread every two seconds without using the Timer object?
        • 1. Re: Run a thread every two seconds
          807605
          Something wrong with using timers?
          • 2. Re: Run a thread every two seconds
            807605
            The Timer generates a new thread which just seems to keep increasing the thread count of my app
            • 3. Re: Run a thread every two seconds
              807605
              What implementation of timer are you using?
              • 4. Re: Run a thread every two seconds
                807605
                final Timer timer = new Timer();
                          
                          int seconds = 2;
                          
                          timer.schedule(new TimerTask() {
                
                               @Override
                               public void run() {
                Executor exec = Executors.newCachedThreadPool();
                                    
                                    
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet1, "Set1", SYNC_CALC_OBJ1));
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet2, "Set2", SYNC_CALC_OBJ2));
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet3, "Set3", SYNC_CALC_OBJ3));
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet4, "Set4", SYNC_CALC_OBJ4));
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet5, "Set5", SYNC_CALC_OBJ5));
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet6, "Set6", SYNC_CALC_OBJ6));
                                    exec.execute(new CalculatorEngine(dirtyPositionsSet7, "Set7", SYNC_CALC_OBJ7));
                                    
                
                
                          }, seconds * 1000, seconds * 1000);
                • 5. Re: Run a thread every two seconds
                  807605
                  To answer my own question: java.util.Timer.
                  • 6. Re: Run a thread every two seconds
                    807605
                    Don't create a new thread pool every time run is called?
                    • 7. Re: Run a thread every two seconds
                      807605
                      D'oh! Silly mistake, thank you for pointing it out!!

                      By the way you may know the answer to this as well?!?

                      How do I accurately gain timings for a thread? These threads are calling CalculatorEngine which locks on an object starts a timer (ctime = System.currentTimeMillis();) and then logs the time just before the thread exits
                      System.out.println("Calculations for " + setName + " for " + calcsMadeByCalcEnginge + " positions took: " + (System.currentTimeMillis() - ctime));
                      But if there is a context swich my timer will continue to run until the thread is reactivated and then completes...
                      • 8. Re: Run a thread every two seconds
                        abillconsl
                        swing programmers should only post on the swing forum.
                        • 9. Re: Run a thread every two seconds
                          807605
                          This is out of my area, but if precise timing is an issue, that may be a job
                          for Real Time Java.

                          http://java.sun.com/javase/technologies/realtime/index.jsp
                          • 10. Re: Run a thread every two seconds
                            abillconsl
                            You want to run a new and different Thread each 2 seconds?
                            • 11. Re: Run a thread every two seconds
                              DrClap
                              Right. That code gives you the elapsed time for a thread. I don't think there's any way to get the CPU time used by a thread, if that's what you are trying to ask for.
                              • 12. Re: Run a thread every two seconds
                                807605
                                So how can I gain accurate timings for the thread?
                                • 13. Re: Run a thread every two seconds
                                  807605
                                  A different implementation implementing waits....

                                  import java.util.*;
                                  import java.text.SimpleDateFormat;
                                  
                                  public class Clock implements Runnable
                                  {
                                  boolean running;
                                  Thread task;
                                  Date date;
                                  
                                  
                                  Clock()
                                  {
                                     date = new Date();
                                     start();
                                  }
                                  
                                  public void start()
                                  {
                                     task = new Thread(this);   
                                     task.start();
                                  }
                                  
                                  
                                  public void stop()
                                  {
                                     running = true;
                                  }
                                  
                                  public void run()
                                  {
                                  long now;
                                  String clock;
                                  SimpleDateFormat dateSyntax = new SimpleDateFormat("s",Locale.getDefault());
                                     dateSyntax.applyPattern("EEE MMM dd HH:mm:ss yyyy");
                                            //---- synch for preemptive wait()
                                     synchronized(this)
                                     {
                                        running = true;
                                        while(running)
                                        {
                                           now = System.currentTimeMillis();
                                           date.setTime(now);
                                           clock = dateSyntax.format(date);
                                           System.out.println(clock);
                                  
                                         //--  Add your code here...
                                          // ........ {Some code} .........
                                       //  (long) timeTake = (long)(System.currentTimeMillis() - (long)now);
                                           try
                                           {
                                              wait(1000);
                                           }
                                           catch(InterruptedException ie)
                                           {
                                           }
                                        }
                                        notifyAll();
                                     }
                                  }
                                  
                                  public static void main(String args[])
                                  {
                                  
                                     try
                                     {
                                        new Clock().task.join();
                                     }
                                     catch(InterruptedException ie)
                                     {
                                     }
                                  }
                                  
                                  }
                                  Anyway thats about as accurate as you will be able to get.
                                  It used to be about +=50MS (not including context switch overhead)
                                  But I haven't tested this recently so it may have improved.
                                  Good Luck!

                                  (T)
                                  • 14. Re: Run a thread every two seconds
                                    807605
                                    Er, did you read the replies? He thought Timer was misbehaving, but it was really his run method at fault.
                                    1 2 Previous Next