Results 1 to 7 of 7

Thread: Timer thread.

  1. #1
    eddie6 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Timer thread.

    I need to know how long my server program has been active so I've created a basic timer. Only problem it uses 90-100% extra CPU once I start this thread. I want to make it only run every second until a minute then only every minute until an hour and then only every hour. I'm just not sure how. Or really anyother way to make it more efficient.

    Thank you so much to anyone who can help me.

    Heres my code

    Java Code:
    private class timeKeeping implements Runnable {
            private boolean running = false;
            private long serverStartTime;
            private ServerUI theInterface;
            public timeKeeping(long startTime, ServerUI theInterface) {
                running = true;
                this.serverStartTime = startTime;
                this.theInterface = theInterface;
            }
            public void run() {
                while(running) {
                    long serverUpTime = System.currentTimeMillis() - serverStartTime;
                    long time;
                    String units;
                    if((serverUpTime/1000) < 60) {
                        time = serverUpTime/1000;
                        units = "seconds";
                    } else if((serverUpTime/1000) > 60 && (serverUpTime/1000) < 3600) {
                        time = (serverUpTime/1000)/60;
                        units = "minutes";
                    } else {
                        time = ((serverUpTime/1000)/60)/60;
                        units = "hours";
                    }
                    theInterface.setServerUpTime(time+" "+units);
                }
            }
            public void stop() {
                running = false;
            }
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    Are you just looking for Thread.sleep()? I'd recommend reading through the API.

    Java Platform SE 6
    And this: http://download.oracle.com/javase/tu...l/concurrency/
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    eddie6 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    worked perfectly thanks!! Just added Thread.sleep(); at the end and it solved the problem. Netbeans does give me a warning saying .sleep called in loop. Does this matter?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    You are passing an argument into sleep(), right? And I don't know what the warning is. Does the program work how you want it to?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    eddie6 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Works fine. yeah I'm sleeping 1000.
    But netbeans says Warning: Thread.sleep called in loop.

    Can't see anything wrong with it tho. Program works fine and CPU is down from about 98% to 2.7%. So I'm happy anyway. Thanks.

  6. #6
    BZwap's Avatar
    BZwap is offline Member
    Join Date
    May 2011
    Location
    Lucknow
    Posts
    8
    Rep Power
    0

    Default

    thanks Kevin

  7. #7
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    netbeans is warning you because it can cause issues when sleeping in a thread (like the program hanging for example) but in your case it is a controlled sleep and you know exactly what is going on and how long it is sleeping so do not worry about it. I also have a timer and get the same warning, it is perfectly OK as long as you are fully aware of what's going on.

Similar Threads

  1. Need help with a timer/thread/loopish thing.
    By AndroidAppNewbie in forum New To Java
    Replies: 7
    Last Post: 02-27-2011, 01:51 PM
  2. system creats unsense Timer thread-0
    By henry123 in forum Advanced Java
    Replies: 0
    Last Post: 02-20-2011, 10:23 AM
  3. Timer won't execute untill daemon thread yields
    By dsollen in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-24-2009, 07:17 AM
  4. 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
  5. Thread vs Timer
    By dawiz001 in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-07-2009, 08:25 PM

Posting Permissions

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