1 2 Previous Next 21 Replies Latest reply: Apr 10, 2007 8:58 AM by 807599 RSS

    problem with thread.sleep();

    807599
      hi i have created a thread to run a method every 3 seconds and to confirm that it is doing so it prints a line in the console. however it seems to be running the method 4 times and then printing the confirmation line twice before sleeping. my code is below, please help

      thanks
      package jdbc;
      import java.sql.SQLException;
      import jdbc.transport;
      /**
       *
       * @author Liam
       */
      public class datagrabberclass implements Runnable 
      {
          
          private Thread ticker;
      
          private int datainsertno = 2;
          /** Creates a new instance of datagrabberclass */
          datagrabberclass(){
              if(ticker == null){
                  ticker = new Thread(this);
                  ticker.start();
                  transport datagrabber = new transport();
              }
              
          }
      
          public void run()  {
              while(true){
                  try {
                      jdbc.transport.getDataAndStoreData();
                      System.out.println("getDataAndStoreData method initialized");
                  } catch (SQLException ex) {
                      ex.printStackTrace();
                  }
                  try {
                      ticker.sleep(3000);
                  } catch (InterruptedException ex) {
                      ex.printStackTrace();
                  }
              
              }
              
                         
                
                          
          }
          }
        • 1. Re: problem with thread.sleep();
          807599
          sleep is a static method. you can't call it on a particular thread and get it to sleep. it puts the thread it's called from to sleep
          • 2. Re: problem with thread.sleep();
            807599
            i thought that the method was called from within the thread its from, i'm confused as to what u mean but thanks
            • 3. Re: problem with thread.sleep();
              807599
              are you saying that i require a second thread to put the ticker thread to sleep?
              • 4. Re: problem with thread.sleep();
                807599
                just call Thread.sleep where you're calling ticker.sleep. looking at it, it should do what you want. let me guess: you're running this in an IDE
                • 5. Re: problem with thread.sleep();
                  807599
                  yes netbeans
                  • 6. Re: problem with thread.sleep();
                    807599
                    ive ried replacing ticker.sleep(3000); with thread .sleep and it still seems to print the method initialized method twice before sleeping
                    • 7. Re: problem with thread.sleep();
                      807599
                      try running it from the command-line. the "console" view in IDEs isn't a real console, it's just a text area. they aren't the most reliable view of what's actually going on, particularly where multi-threading is involved. I suspect your code is working just fine, but the IDE is screwing up the output
                      • 8. Re: problem with thread.sleep();
                        807599
                        i thought that might be the case but the method which is called sends certain dtat to a dtatbase, i ve checked the actual database the the method is being called 4 times before the thread sleeps
                        • 9. Re: problem with thread.sleep();
                          807599
                          i thought that might be the case but the method which
                          is called sends certain dtat to a dtatbase, i ve
                          checked the actual database the the method is being
                          called 4 times before the thread sleeps
                          have the run() method also print out a timestamp. see what it says. this sounds odd, you must be doing something else you're not telling me about!
                          • 10. Re: problem with thread.sleep();
                            807599
                            sorry im pretty new to java how do u get timestamp printed
                            • 11. Re: problem with thread.sleep();
                              807599
                              these are all the related classes
                               public Main () {
                                      
                                    
                                    
                                  }
                                  
                                  /**
                                   * @param args the command line arguments
                                   *
                                   */
                                  
                                 
                                  
                                  public static void main(String[] args) throws SQLException {
                                  
                                      
                                      datagrabberclass tickerthread = new datagrabberclass();
                                      
                                      tickerthread.run();
                                      
                                      
                                
                                  }
                                  
                                  
                                           
                                  
                              }
                              package jdbc;
                              import java.sql.*;
                              /**
                               *
                               * @author Liam
                               */
                              public class storedata {
                                  
                                  /** Creates a new instance of storedata */
                                  
                                  
                                  public static void storeData(String table, double CLOSE,double MA,double VOL10,double VOL100,double VOL1000) throws SQLException{
                                        try {
                                   
                                          Class.forName("org.apache.derby.jdbc.ClientDriver");
                                          Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/fx");
                                          String url = "jdbc:derby://localhost:1527/fx";
                                          
                                                 
                                          Connection con = DriverManager.getConnection(url, null, null);
                                          
                                         
                                         PreparedStatement prep = con.prepareStatement(
                                  "INSERT into " + table + " values (?, ?, ?, ?, ?)");
                                         prep.setDouble(1,CLOSE);
                                         prep.setDouble(2,MA);
                                         prep.setDouble(3,VOL10);
                                         prep.setDouble(4,VOL100);
                                         prep.setDouble(5,VOL1000); 
                                         prep.executeUpdate();
                                         con.close();
                              
                              
                                      } catch (ClassNotFoundException ex) {
                                          ex.printStackTrace();
                                      }
                                        
                                        
                                  }
                              
                              package jdbc;
                              
                              import java.sql.SQLException;
                              import java.lang.*;
                              
                              /**
                               *
                               * @author Liam
                               */
                              public class transport extends storedata {
                                  
                                  
                                    
                                  
                                  
                                  
                                  static void getDataAndStoreData() throws SQLException {
                                      
                                      double usdjpyClose = 5.6;
                                      double usdjpyMa = 0.0;
                                      double usdjpyLiam10 = 0.0;
                                      double usdjpyLiam100 = 0.0;
                                      double usdjpyLiam1000 = 0.0;
                                      String usdjpyTable = "usdjpy";
                                      
                                      double usdchfClose = 0.0;
                                      double usdchfMa = 0.0;
                                      double usdchfLiam10 = 0.0;
                                      double usdchfLiam100 = 0.0;
                                      double usdchfLiam1000 = 0.0;
                                      String usdchfTable = "usdchf";
                                      
                                      double eurusdClose = 0.0;
                                      double eurusdMa = 0.0;
                                      double eurusdLiam10 = 0.0;
                                      double eurusdLiam100 = 0.0;
                                      double eurusdLiam1000 = 0.0;
                                      String eurusdTable = "eurusd";
                                      
                                      double gbpusdClose = 0.0;
                                      double gbpusdMa = 0.0;
                                      double gbpusdLiam10 = 0.0;
                                      double gbpusdLiam100 = 0.0;
                                      double gbpusdLiam1000 = 7.9;
                                      String gbpusdTable = "gbpusd";
                                      
                                      //code to obtain data and reassign to method
                                      
                                      storeData(usdjpyTable,usdjpyClose,usdjpyMa,usdjpyLiam10,usdjpyLiam100, usdjpyLiam1000);
                                      storeData(usdchfTable,usdchfClose,usdchfMa,usdchfLiam10,usdchfLiam100, usdchfLiam1000);
                                      storeData(eurusdTable,eurusdClose,eurusdMa,eurusdLiam10,eurusdLiam100, eurusdLiam1000);
                                      storeData(gbpusdTable,gbpusdClose,gbpusdMa,gbpusdLiam10,gbpusdLiam100, gbpusdLiam1000);
                                      
                                      
                                      
                                      
                                  }
                                  }
                              
                              package jdbc;
                              import java.sql.SQLException;
                              import jdbc.transport;
                              /**
                               *
                               * @author Liam
                               */
                              public class datagrabberclass implements Runnable 
                              {
                                  
                                  private Thread ticker;
                              
                                  private int datainsertno = 2;
                                  /** Creates a new instance of datagrabberclass */
                                  datagrabberclass(){
                                      if(ticker == null){
                                          ticker = new Thread(this);
                                          ticker.start();
                                          transport datagrabber = new transport();
                                      }
                                      
                                  }
                              
                                  public void run()  {
                                      while(true){
                                          try {
                                              jdbc.transport.getDataAndStoreData();
                                              System.out.println("getDataAndStoreData method initialized");
                                              
                                          } catch (SQLException ex) {
                                              ex.printStackTrace();
                                          }
                                          try {
                                              Thread.sleep(3000);
                                          } catch (InterruptedException ex) {
                                              ex.printStackTrace();
                                          }
                                      
                                      }
                                      
                                                 
                                        
                                                  
                                  }
                                  }
                              the database in which im sending data to contains 4 tables
                              • 12. Re: problem with thread.sleep();
                                807599
                                easiest way is
                                System.out.println(System.currentTimeMillis());
                                that'll do for this test
                                • 13. Re: problem with thread.sleep();
                                  807599
                                  ok i did it here are the results from the console:

                                  init:
                                  deps-jar:
                                  compile:
                                  run:
                                  getDataAndStoreData method initialized
                                  1176211519955
                                  getDataAndStoreData method initialized
                                  1176211519963
                                  getDataAndStoreData method initialized
                                  1176211523020
                                  getDataAndStoreData method initialized
                                  1176211523033
                                  getDataAndStoreData method initialized
                                  1176211526084
                                  getDataAndStoreData method initialized
                                  1176211526102
                                  getDataAndStoreData method initialized
                                  1176211529123
                                  getDataAndStoreData method initialized
                                  1176211529147

                                  it prints 2 lines just milliseconds away from each other that waits for the 3000
                                  • 14. Re: problem with thread.sleep();
                                    807599
                                    as well as printing out the timestamp, print out the result of Thread.currentThread().getId()

                                    looks like you're creating multiple threads. 4, at a guess!
                                    1 2 Previous Next