[concurrency-interest] TimerTask with PooledExecutor

trebor iksrazal iksrazal@yahoo.com
Wed, 30 Jun 2004 08:01:48 -0700 (PDT)


Inside my runnable I want to perform a task that may
time out. I searched this list and came up with the
following: 

import java.util.*;
import java.io.*;

import EDU.oswego.cs.dl.util.concurrent.*;

public class PoolTest 
{
  class MyRunnable implements Runnable
  {
    public void run()
    {
      java.util.TimerTask task = new TimerTask()
      {
        Thread thread = Thread.currentThread();
        public void run()
        {
          System.out.println("Inside TimerTask
run...");
          thread.interrupt(); // interrupt work
        }
      };
 
      Timer timer = new Timer();
      timer.schedule(task, 3000);
      try
      {
        // do interruptible work ...
        System.out.println("Inside MyRunnable...");
      }
      finally
      {
        task.cancel();
        Thread.interrupted(); // clear interrupt flag
      }
    }
  }

  public static void main(String args[])
  {
    new PoolTest();
  }

  public PoolTest() 
  {
    try
    {
      PooledExecutor pe = new PooledExecutor(3);
      pe.execute(new MyRunnable());
     
pe.shutdownAfterProcessingCurrentlyQueuedTasks();
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    }
  }
}

Unfortunately the Runnable never completes or times
out. This could be due to a lack of Thread.start(),
typically in the form:

Thread task = new Thread(new Task());
TimerOut to = new TimerOut(task);
new Timer(true).schedule((TimerTask) to,TIMEOUT);
task.start();

Yet I can't call Thread.start() in the Runnable, I
don't think. 

Any ideas?
iksrazal



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail