aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes resetting a Thread loop timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "resetting a Thread loop timer" Watch "resetting a Thread loop timer" New topic
Author

resetting a Thread loop timer

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
I probably shouldn't be asking this in this forum, but since I'm studying for the exam and actually using a Thread in real life code I figured I'd ask this question here first.
For some background, I have a singleton class that other users of this web application share by calling a getInstance method in the class. Upon the first time the getInstance method is called if the object it null it goes ahead and initializes itself.
What I'm working on is after a certain amount of time without the getInstance method being called I want the object instance to be set back to null ( reason being for Data Base connection stuff going on ). I'm currently thinking of achieving this by spawning a Thread that has some counter loop activity. Then whenever the getInstance method is called it will reset the loop so the Thread loop just starts over again. Eventually if it gets to the end of the loop, it will set the class to null and exit the Thread. The Run method in looks something like this:
<PRE STYLE="color: blue; size 13px;">
public void run() {

for ( long i = 0; i < 6005000; i++ ) {
if ( reset ) {
i = 0; //counter starts over;
resetCounter( false ); // so counter will continue until reset set to true again
}
}
instance = null;
}
</PRE>
Everytime the getInstance method is called it sets the boolean reset to true so that the loop will start over again.
What I'm wondering is if there is a better way to do this with maybe using the sleep() method? If I used the sleep() method though is there a way to tell the Thread "Hey, start your sleeping over again from the beginning?"
Thanks for any help.
Rick
[This message has been edited by Rick Reumann (edited December 21, 2001).]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

What I'm wondering is if there is a better way to do this with maybe using the sleep() method? If I used the sleep() method though is there a way to tell the Thread "Hey, start your sleeping over again from the beginning?"

Rick,
You definitely want to use the Thread.sleep method in this case. Thread.sleep throws an InterruptedException when it is interrupted. At that point, you can handle the exception and then go on with life. In your case, I think something like this might work well (untested code):

I think that should do what you want. Not only is this a little bit simpler, but, by calling sleep, this thread will give up the processor prematurely to other threads that are actually doing something useful. Whenever you want to reset the timer, simply interrupt this thread by calling its interrupt() method.
You may want to look closely at what will happen when you exit your application, though. If you run this thread as a daemon thread, it should end as soon as the application does but, if not, it may continue forever. I'll let you look into that one in more detail. If you need help, I guess that's just another question to post!
I hope this helps you out (and remember to test that code before using it, I was just writing it as I went).
Corey
[This message has been edited by Corey McGlone (edited December 21, 2001).]


SCJP Tipline, etc.
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281

I think that should do what you want.

</BLOCKQUOTE>
THANK YOU! This was a perfect solution! I never thought of interrupting the sleep method. I know this wasn't exactly a certification quesiton per se, but to all those out there studying it really does help to try real world examples. I know so much more about Threading stuff that could come up on the test after trying some things and then posting the questios here and getting helpful answers like I just received from Corey
For those that care the real life example in the singleton works liek this:
<PRE style="color: blue; font-size: 13px;">
private static CorporateRepository instance;
private static Thread t = null;
public static CorporateRepository getInstance() throws CorporateRepositoryException
{
if ( instance == null ) {
instance = new CorporateRepository();
t = new Thread( instance );
t.setDaemon(true);
t.start();
}
else {
t.interrupt();
}
return instance;
}

public void run() {

while ( true )
{
boolean reset = false;
try
{
Thread.sleep(600000);
}
catch (InterruptedException e)
{
reset = true;
}
if ( !reset ) {
instance = null;
break;
}
}
}
</PRE>
( I added the break Corey since if the instance gets sets to null the Thread will end up being recreated upon the next call to getInstance, so I wanted the Thread to exit after the certain amount of time. I also set it as a Daemon thread just in case. Thanks again. )[/b]
</BLOCKQUOTE>

[This message has been edited by Rick Reumann (edited December 21, 2001).]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I also set it as a Daemon thread just in case.

I'm glad this worked for you. Remember, setting a thread to a daemon thread isn't always the best way to kill a thread. Obviously, the thread will then die when the main thread quits, but you don't get to "clean up" any resources used by that daemon thread. It simply dies. In this case, I don't think it really matters, as your timer thread doesn't really have any resources that it's holding. But, imagine if that thread had its own database connection. You'd probably want to release that connection before killing the thread. If you simply made the thread a daemon thread and then snuffed it, you'd have no chance to release the resources. This is the general "proper" way to kill a thread:

Thread also has an isInterrupted() that you can use, if you're not planning on using the sleep() method.
Just more threading tidbits as you get ready for the exam.
Best of luck,
Corey

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
After making that last post, I came up with a question of my own to ask. Why is the "proper" way of cleaning up a thread (or at least the most often used, in my experience) to catch an InterrupedException and then free up resources in the catch or finally block? After all, that requires the main thread to explicitly interrupt each of the spawned threads in order to get them to release their resources.
Why can't you just override the Object.finalize() method to clean those resources up when they are garbage collected? I'm no garbage collection expert, but I don't see why this wouldn't work, and it doesn't require the main thread to interrupt the spawned threads - it could literally "fire and forget" those threads (assuming it doesn't need to communicate with them).
Is there a reason this isn't used? Or is this used and I just haven't noticed it?
Thanks,
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Why don't you use a javax.swing.Timer ?
It's much more easier to reuse things that have already been developed than reinvent the wheel every time, don't you think?
You can even look at the source of Timer to see how they do it...
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by Valentin Crettaz:
Why don't you use a javax.swing.Timer ?
It's much more easier to reuse things that have already been developed than reinvent the wheel every time, don't you think?
You can even look at the source of Timer to see how they do it...

Valentin,
Good suggestion. As I look into it, however, I'm a little stumped by something. According to what I've been reading I should be able to use the util.Timer and util.TimerTask class to do what I want since I'm not really in a GUI app. However the swing.Timer class looks like it has some more useful functionality that I can't seem to implement using the util.Timer ( I should be able to use util.Timer I'm sure I'm just screwing up ). The deal is I want to be able to start the Timer over when a certain condition occurs. The swing.Timer has the method restart() which would be perfect. Does anyone know how I could achieve this using the util.Timer and TimerTask classes? Maybe someone could email me off list at rick_reumann@gmx.net since I know this has gone beyond the Cert forum topic. Thanks so much.
Rick
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
javax.swing.Timer has nothing to do with GUI, even though it is in the Swing package you can used it in non-GUI apps... and moreover it is far more easier to use than the java.util.Timer.
Do you want me to move this to another forum where you may get more answers?

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 21, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: resetting a Thread loop timer
 
Similar Threads
I want to demonstrate an unsafe thread
Threads - join()
A few questions on wait(), sleep(int) and InterruptedException
Marcus Green Mock Exam 1
ServletContextListener & Threads