Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Eranga

Thread: How to use timer to monitor lengthy loop progress

  1. #1
    JotaPe is offline Member
    Join Date
    Jun 2009
    Location
    Madrid
    Posts
    2
    Rep Power
    0

    Default How to use timer to monitor lengthy loop progress

    Hi,

    I have a long process that iterates through a large data set and I need to monitor its progress.

    Right now it's something like:

    Read recordSet
    for (...index++) {
    ... process record[index];
    if (index modulus 500 = 0) {log to stdout}
    }


    I'm using stdout since that's where the output from the Garbage Collector is gathered, so I can check how the loop is behaving memory-wise.

    In other languages I'd use a timer instead of the ugly 'if (...' but I am quite new and lost in Java, and I have been unable to find any suitable examples.

    How can I use a timer in this context so that the logging is triggered each 30s, for instance? The loop might run for a veeery long time (20-60 minutes).

    I understand that this would require (at least!):
    - Setting up the timer
    - Starting the loop
    - Getting some info about the loop status, in the timer callback
    - Shutting down/Killing the timer when the loop ends (or fails...)

    Can anyone help?

    Thanks to all.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    JotaPe is offline Member
    Join Date
    Jun 2009
    Location
    Madrid
    Posts
    2
    Rep Power
    0

    Default

    Thanks Eranga!

    I guess that I did not provide enough information about the setup of my app. Sorry for that!

    These processes are run in a server (no GUI is involved). They are automated queries to a rather large database about books and the result of each one is a file (in the order of 50-100MB) which is sent back to the corresponding user via ftp.

    Several such loops might be running simultaneously, each in its own thread.

    Would Swing be a good choice for such a setup?

    Thanks again!

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Swing timers would be fine. Either the Swing Timer or the Utility Timer (javax.swing.Timer and java.util.Timer) would work. If all you are doing is logging data to a stream, you should be fine with either, but if you have lengthy actions after the timer completes, the Utility Timer would probably be better.

    Good Luck with whatever you choose,
    Singing Boyo

    EDIT: Did another quick read of your original post. If you want to log data every 30 times it loops, That would probably have to be done differently.
    Last edited by Singing Boyo; 06-26-2009 at 12:27 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    448
    Rep Power
    6

    Default

    You can use system time to find out your loop process execution
    Mak
    (Living @ Virtual World)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JotaPe View Post
    These processes are run in a server (no GUI is involved).
    Actually event without involve of UI you can use swing timer. You have to design that. Just look at the following example.

    Java Code:
    import java.awt.event.*;
    import javax.swing.Timer;
    
    /**
     *
     * @author Eranga Thennakoon
     */
    public class SwingTimerDemo {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // Initialize the timer object, in interval of 1 second
            Timer MyTimer = new Timer(1000, new MyTimerActionListener());
    
            // Start the timer
            MyTimer.start();
    
            // Keep the main thread qite for 10 seconds
            try {
            Thread.sleep(10000);
            }
            catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    
            // Stop the timer at the end
            MyTimer.stop();
        }
    }
    
    class MyTimerActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Swing Timer");
        }
    }
    I think you can understand the above code, if not let me know. I'll explain it again.

    Ya I think it's the best way you have to take.
    Vbabey likes this.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JotaPe View Post
    Several such loops might be running simultaneously, each in its own thread.
    You mean that, you have separate thread for each loop?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by makpandian View Post
    You can use system time to find out your loop process execution
    But I think he wants to find the time interval. Still you want a way to tick the time.

Similar Threads

  1. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  2. How to use Progress bar
    By LankanSniper in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 10:44 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  4. Console Progress Bar
    By new_2_java in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •