Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Pausing in Java?

Pausing in Java?
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jan 1, 2002, 12:17 PM
 
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
Reply With Quote
Jan 1, 2002, 01:29 PM
 
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
Reply With Quote
Jan 1, 2002, 03:05 PM
 
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
Reply With Quote
Jan 1, 2002, 03:10 PM
 
nevermind, i just removed CurrentThread and it works perfectly. Thanks!
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jan 1, 2002, 03:38 PM
 
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
Reply With Quote
Jan 2, 2002, 06:12 PM
 
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
Reply With Quote
Jan 2, 2002, 06:19 PM
 
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
Reply With Quote
Jan 2, 2002, 06:21 PM
 
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
Reply With Quote
Jan 2, 2002, 06:23 PM
 
That previous class should extend TimerTask, not implement it. Sorry.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:26 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2