Re: Threading design question
Big Jim wrote:
// listener is the thread that listens for prompts
// "this" is also a thread allowing the listener to interrupt us when it
gets a prompt
listener.initialise(this);
listener.start();
while(true)
{
try
{
Thread.sleep(pollSeconds * 1000);
if this object extends Thread, you don't need to prefix it.
if its a Runnable, using Thread.getCurrent().sleep() is the correct way.
}
catch(InterruptedException ex)
{
log.info("Interrupted ...");
}
publisher.doProcessing();
}
So, basically I'm using the Interrupted exception to break out of the sleep
early.
I think this is a wrong interpretation., IE is mandatory and only used
in case something causes it to interrupt as en error sort of. What I
mean to say is that you do not control what interrupts it, unless you
have some code you are not showing us.
What I'm wondering is whether this is a poor way to do this as I'm using the
exception handling mechanism for normal processing i.e. it's not an
exceptional situation. Is this poor? Is there a more standard way to do
this?
I'm sure there is a pattern for this kind of task, I just cant recall
what its named.
You could use a job scheduler such as Quartz for this, unless its to
much for just one single task.
Or you could make a design of it yourself, where you integrate a sort of
messaging queue (if you need this to work for several tasks), where the
queue request blocks for x ms or is checked every x ms with an
occasional execution of the job if nothing is in the queue.
Since you are talking about waiting for seconds and there only seems to
be one job, the only thing you need to do is integrate the prompting
into the loop as describe in the above paragraph.
tom