 |
 |
Pausing in Java?
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: New York
Status:
Offline
|
|
I'm doing a CS assignment that monitors network traffic, or slowness. I have this small java app that downloads a webpage and prints out the time that it took to do it. I have this loop running forever. However it would be really nice if I could get it to only load the page once a minute. How can I make the application pause for a minute?
Thanks.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
try{java.lang.Thread.currentThread().sleep(60000)}
catch(Exception e){}
That should sleep the current thread for 60,000 milliseconds (or 60 seconds).
An easier way would be to use an NSTimer and have it fire every minute:
NSTimer timer = new NSTimer(60,this,new NSSelector("methodName",new Class[]{this.getClass()}),this,true);
NSRunLoop.currentRunLoop().addTimerForMode(timer,N SRunLoop.DefaultRunLoopMode);
The former locks your app for a minute, the later calls a method every minute.
HTH,
F-bacher[/LIST]
[ 01-01-2002: Message edited by: Ghoser777 ]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: New York
Status:
Offline
|
|
OK thanks but I'm not using threads. I like the first way better since I can just stick it in there, but how would I do it if my app isn't threaded?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: New York
Status:
Offline
|
|
nevermind, i just removed CurrentThread and it works perfectly. Thanks!
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Oh sorry, there was suppose to be parenthesis at the end of current thread... but u said it worked when getting rid of currentThread? I didn't think you could call that method from a stati context. Oh well.
Atleast it works now,
F-bacher
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
Thread.sleep() always refers to the current thread. When you call Thread.currentThread() to get a thread reference, you are STILL calling a static method even though you are invoking it on an object.
BTW, whether you know it or not, EVERY java program is threaded by definition. main() is invoked by the main thread, and any other thread you create (or if you open an AWT window or invoke a method which creates a thread) will make your program multi-threaded.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
Forget this post. Don't try to edit a post with code in it! UBB could sure be smarter...
[ 01-02-2002: Message edited by: absmiths ]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
Originally posted by Ghoser777:
<STRONG>An easier way would be to use an NSTimer and have it fire every minute:
NSTimer timer = new NSTimer(60,this,new NSSelector("methodName",new Class[]{this.getClass()}),this,true);
NSRunLoop.currentRunLoop().addTimerForMode(timer,N SRunLoop.DefaultRunLoopMode);
</STRONG>
Why not do the same using Java libraries?
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
import java.util.Timer;
import java.util.TimerTask;
class Timeable implements TimerTask
{
public void run()
{
<font color = brown>//do your every-minute task.</font>
}
public static void main(String [] argv)
{
<font color = brown>// create a timer</font>
Timer timer = new Timer();
<font color = brown>// schedule an instance of this class</font>
<font color = brown>// to run every minute, with no initial delay</font>
timer.schedule(new Timeable(), <font color = blue>0</font>, <font color = blue>60000</font>);
}
}
</font>[/code]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
That previous class should extend TimerTask, not implement it. Sorry.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|