Click to See Complete Forum and Search --> : Java - threads, periodic events


klamath
12-12-2000, 01:17 PM
I'm writing a multi-threaded network daemon in Java. I'm still in the 'design' stage, but I can't figure out how to do a particular task. Here's the scenario:

I have a collection of cached data (probably in a HashMap, with 2 fields: the data, and a timestamp of when the data was last generated). Once in a while (lets say every 10 minutes), I want to spawn a thread which iterates through the items in the cache, and removes the old ones.

How do you do this? The main code of the daemon revolves around listening on a ServerSocket, and spawning threads to handle incoming connections. I'd rather not check the time on *every* incoming connection, because I'd like to handle hundreds of connections per second. Also, if I used something like this and there were no connections for 30 minutes, the maintainence stuff would not be done during that time. I could create the maintainence thread permanently and just get it to sleep() between use, but it would not be efficient to use an entire thread for this stuff, which is only done occaisonally and will only take 3 or 4 seconds to complete, at most.

Any ideas? Thanks in advance.

edit: Fix typos

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the Better Bulletin Board (http://bbb.sourceforge.net)

[This message has been edited by klamath (edited 12 December 2000).]

Stuka
12-12-2000, 01:42 PM
What might help you here is the Reactor design pattern. What that pattern essentially does is react to ANY registered event type (socket I/O, timer, etc.) by calling an appropriate event handler. In other words, make your main daemon be/contain a reactor object, register appropriate handlers for your socket I/O and a timer, then let 'er rip. I'd tell ya more about the pattern, but that's about the extent of my knowledge. You can check out the ACE web site www.cs.wustl.edu/~schmidt/ACE (http://www.cs.wustl/~schmidt/ACE) for more info on the design pattern. <edit> fixed the link </edit>

[This message has been edited by Stuka (edited 12 December 2000).]

nanode
12-12-2000, 04:16 PM
This is a bit different, but I am working on a visual form editor in Java that makes a lot of use of PropertyChangeListeners.

This a rough idea for your situation:

each iteration can send a message to some external class object. Depending on the values or context of that message, the external class obj. can take action: perhaps send a message back to your main app. telling it to call a local method.
That external class could keep track of time, count events or any number of things. You might call it an event accountant.

This might seem really odd, but what you gain is a cohesive design with a lot of functionality, without compilcating your existing code.

Pardon me if I totally missed on this. I'm a relative rookie. http://www.linuxnewbie.org/ubb/smile.gif

klamath
12-13-2000, 12:53 PM
Thanks for the suggestions guys. I'm still looking into this: one possibility is using the java.util.Timer class. I think that should do everything I need.

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)
Looking for an open source project to contribute to? Check out the Better Bulletin Board (http://bbb.sourceforge.net)