[concurrency-interest] Example using TimedCallable with PooledExecutor

Joseph Bowbeer jozart@csi.com
Sun, 8 Jun 2003 01:34:20 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_003C_01C32D5E.1545FD70
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Michael,

Here's a version that uses a single java.util.Timer thread to handle the =
cancellations.

Timer timer =3D new Timer();

Submit command to an executor.  When it runs, it schedules a timer task =
to interrupt it when the allotted time has expired.  When it finishes, =
it cancels the timer task and clears the interrupt flag (just in case an =
interrupt from the timer task went unnoticed).

This version creates fewer threads than TimedCallable, but relies more =
on the command to be responsive to interrupts.

Runnable command =3D new Runnable() {
    public void run() {
        TimerTask task =3D new TimerTask() {
            Thread thread =3D Thread.currentThread();
            public void run() {
                thread.interrupt(); // interrupt work
            }
        }
        timer.schedule(task, timeout);
        try {
            // do interruptible work ...
        } finally {
            task.cancel();
            Thread.interrupted(); // clear interrupt flag
        }
    }
}


  ----- Original Message -----=20
  From: Joseph Bowbeer=20
  To: michael.mattox@verideon.com ; =
concurrency-interest@altair.cs.oswego.edu=20
  Sent: Friday, June 06, 2003 2:26 PM
  Subject: Re: [concurrency-interest] Example using TimedCallable with =
PooledExecutor


  Michael,

  TimedCallable is designed for Callables (which can be viewed as =
"super" Runnables).

  For simple Runnables you can use thread.join() as follows:

    Thread thread =3D new Thread(runnable);
    thread.start();
    try {
      thread.join(timeout);
    } catch (InterruptedException ex) {
      /* Stop thread if we were interrupted or timed-out
       * while waiting for the result.
       */
      thread.interrupt();
      throw ex;
    }

  This doubles the thread count in your system.  If you do this a lot, I =
recommend handling the timeouts on a single timer thread.

  Also, note that thread.interrupt() may not do anything -- depending on =
what the runnable is doing at the time and whether it was written to be =
responsive to interrupts.



  ----- Original Message -----=20
  From: "Michael Mattox" <michael.mattox@verideon.com>
  To: <concurrency-interest@altair.cs.oswego.edu>
  Sent: Friday, June 06, 2003 8:31 AM
  Subject: [concurrency-interest] Example using TimedCallable with =
PooledExecutor



  I'm using a PooledExecuter but sometimes my threads hang forever and I =
want
  to add a timeout.  I looked at the TimedCallable class but it's not =
clear
  how to use it for my particular example (I tried the example in the
  FutureResult class).  Can someone give me a hint on how to use this?

  private boolean addMonitorToQueue(String monitorId) {
  boolean success =3D false;
  DispatcherThread thread;
  try {
  thread =3D new DispatcherThread(monitorId);
  if (!queue.contains(thread)) {
  try {
  pool.execute(thread);
  success =3D true;
  log.debug("Added monitor " + monitorId + " to the queue.");
  } catch (InterruptedException e) {
  log.error("Could not add thread for monitor " + monitorId + " : ", e);
  }
  } else {
  log.debug("Did not add monitor " + monitorId + " to the queue because
  it's already in the queue. ");
  }
  } catch (TechnicalException e) {
  log.error("Could not dispatch thread for monitor: " + monitorId, e);
  }
  return success;
  }

  Thanks!
  Michael

------=_NextPart_000_003C_01C32D5E.1545FD70
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Michael,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Here's a version that uses a single =
java.util.Timer=20
thread to handle the cancellations.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Timer timer =3D new =
Timer();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Submit command to an executor.&nbsp; =
When it runs,=20
it schedules a timer task to interrupt it when the allotted time has=20
expired.&nbsp; When it finishes, it cancels the timer task and clears =
the=20
interrupt flag (just in case an interrupt from the timer task went=20
unnoticed).</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>This version creates&nbsp;fewer threads than TimedCallable, but =
relies more=20
on the command to be responsive to interrupts.</DIV>
<DIV><FONT face=3DArial =
size=3D2></FONT>&nbsp;</DIV>Runnable&nbsp;command =3D new=20
Runnable() {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; public void run() =
{</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
TimerTask=20
task =3D new TimerTask() {</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p; Thread=20
thread =3D Thread.currentThread();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; public void run() {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; thread.interrupt(); // interrupt=20
work</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
timer.schedule(task, timeout);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
try=20
{</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; // do interruptible work ...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } =
finally=20
{</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; task.cancel();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; Thread.interrupted(); // clear interrupt =
flag</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Djozart@csi.com href=3D"mailto:jozart@csi.com">Joseph =
Bowbeer</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dmichael.mattox@verideon.com=20
  =
href=3D"mailto:michael.mattox@verideon.com">michael.mattox@verideon.com</=
A> ; <A=20
  title=3Dconcurrency-interest@altair.cs.oswego.edu=20
  =
href=3D"mailto:concurrency-interest@altair.cs.oswego.edu">concurrency-int=
erest@altair.cs.oswego.edu</A>=20
  </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, June 06, 2003 =
2:26 PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: =
[concurrency-interest]=20
  Example using TimedCallable with PooledExecutor</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial size=3D2>
  <DIV>Michael,</DIV>
  <DIV>&nbsp;</DIV>
  <DIV>TimedCallable is designed for Callables (which can be viewed as =
"super"=20
  Runnables).</DIV>
  <DIV>&nbsp;</DIV>
  <DIV>For simple Runnables you can use thread.join() as follows:</DIV>
  <DIV>&nbsp;</DIV></FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp; Thread thread =3D new=20
  Thread(runnable);</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp; thread.start();</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp; try {</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;=20
  thread.join(timeout);</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp; }&nbsp;</FONT><FONT =
face=3DArial=20
  size=3D2>catch (InterruptedException ex) {</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; /* Stop thread if =
we were=20
  interrupted or timed-out</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp; * while =
waiting for the=20
  result.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp; =
*/</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;=20
  thread.interrupt();</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; throw =
ex;</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp; }</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>This doubles the thread count&nbsp;in =
your=20
  system.&nbsp; If you do this a lot, I recommend handling the timeouts =
on a=20
  single timer thread.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>Also, note that thread.interrupt() =
may not do=20
  anything -- depending on what the runnable is doing at the time and =
whether it=20
  was written to be responsive to interrupts.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>----- Original Message ----- </FONT>
  <DIV><FONT face=3DArial size=3D2>From: "Michael Mattox" &lt;</FONT><A=20
  href=3D"mailto:michael.mattox@verideon.com"><FONT face=3DArial=20
  size=3D2>michael.mattox@verideon.com</FONT></A><FONT face=3DArial=20
  size=3D2>&gt;</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>To: &lt;</FONT><A=20
  href=3D"mailto:concurrency-interest@altair.cs.oswego.edu"><FONT =
face=3DArial=20
  size=3D2>concurrency-interest@altair.cs.oswego.edu</FONT></A><FONT =
face=3DArial=20
  size=3D2>&gt;</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>Sent: Friday, June 06, 2003 8:31 =
AM</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>Subject: [concurrency-interest] =
Example using=20
  TimedCallable with PooledExecutor</FONT></DIV></DIV>
  <DIV><FONT face=3DArial><BR><FONT =
size=3D2></FONT></FONT></DIV><BR><FONT=20
  face=3DArial size=3D2>I'm using a PooledExecuter but sometimes my =
threads hang=20
  forever and I want<BR>to add a timeout.&nbsp; I looked at the =
TimedCallable=20
  class but it's not clear<BR>how to use it for my particular example (I =
tried=20
  the example in the<BR>FutureResult class).&nbsp; Can someone give me a =
hint on=20
  how to use this?<BR><BR>private boolean addMonitorToQueue(String =
monitorId)=20
  {<BR>boolean success =3D false;<BR>DispatcherThread thread;<BR>try =
{<BR>thread =3D=20
  new DispatcherThread(monitorId);<BR>if (!queue.contains(thread)) =
{<BR>try=20
  {<BR>pool.execute(thread);<BR>success =3D true;<BR>log.debug("Added =
monitor " +=20
  monitorId + " to the queue.");<BR>} catch (InterruptedException e)=20
  {<BR>log.error("Could not add thread for monitor " + monitorId + " : =
",=20
  e);<BR>}<BR>} else {<BR>log.debug("Did not add monitor " + monitorId + =
" to=20
  the queue because<BR>it's already in the queue. ");<BR>}<BR>} catch=20
  (TechnicalException e) {<BR>log.error("Could not dispatch thread for =
monitor:=20
  " + monitorId, e);<BR>}<BR>return=20
success;<BR>}<BR><BR>Thanks!<BR>Michael<BR></BLOCKQUOTE></FONT></BODY></H=
TML>

------=_NextPart_000_003C_01C32D5E.1545FD70--