Results 1 to 7 of 7
Thread: Timer thread.
- 04-04-2011, 03:24 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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; } }
- 04-04-2011, 03:48 PM #2
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-04-2011, 03:57 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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?
- 04-04-2011, 04:53 PM #4
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-04-2011, 04:56 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 05-03-2011, 02:29 PM #6
thanks Kevin
- 05-21-2011, 09:25 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 80
- Rep Power
- 0
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
-
Need help with a timer/thread/loopish thing.
By AndroidAppNewbie in forum New To JavaReplies: 7Last Post: 02-27-2011, 02:51 PM -
system creats unsense Timer thread-0
By henry123 in forum Advanced JavaReplies: 0Last Post: 02-20-2011, 11:23 AM -
Timer won't execute untill daemon thread yields
By dsollen in forum Threads and SynchronizationReplies: 1Last Post: 12-24-2009, 08:17 AM -
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 02:45 AM -
Thread vs Timer
By dawiz001 in forum Threads and SynchronizationReplies: 5Last Post: 03-07-2009, 09:25 PM
Bookmarks