TimerTask inside Runnable?

Discussion in 'Java' started by iksrazal, Jun 29, 2004.

  1. iksrazal

    iksrazal Guest

    I want to do something like the following:

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

    import EDU.oswego.cs.dl.util.concurrent.Executor;
    import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
    import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;

    public class PoolTest
    {
    class MyRunnable implements Runnable
    {
    public void run()
    {
    java.util.TimerTask task = new TimerTask()
    {
    Thread thread = Thread.currentThread();
    public void 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();
    }
    }
    }

    The thread pool calls start() itself, so I can't call
    TimerTask.start() . This code never exits.

    Any ideas?
    iksrazal
     
    iksrazal, Jun 29, 2004
    #1
    1. Advertising

  2. iksrazal

    Liz Guest

    "iksrazal" <> wrote in message
    news:...
    > I want to do something like the following:
    >
    > import java.util.*;
    > import java.io.*;
    >
    > import EDU.oswego.cs.dl.util.concurrent.Executor;
    > import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
    > import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;
    >
    > public class PoolTest
    > {
    > class MyRunnable implements Runnable
    > {
    > public void run()
    > {
    > java.util.TimerTask task = new TimerTask()
    > {
    > Thread thread = Thread.currentThread();
    > public void 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();
    > }
    > }
    > }
    >
    > The thread pool calls start() itself, so I can't call
    > TimerTask.start() . This code never exits.
    >
    > Any ideas?
    > iksrazal


    kludge this idea
    ---
    // class to create a JLable and put the time of day into it (refresh every
    1 second)
    static class ClockLabel extends JLabel implements ActionListener
    {
    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
    ClockLabel(String text, int horizontalAlignment)
    {
    super(text, horizontalAlignment);
    Timer t = new Timer(1000, this);
    t.start();
    }
    public void actionPerformed(ActionEvent e)
    {
    setText((formatter.format(new Date())).toString());
    }
    }
     
    Liz, Jun 29, 2004
    #2
    1. Advertising

  3. iksrazal

    iksrazal Guest

    "Liz" <> wrote in message news:<0RjEc.3483$AI.2726@attbi_s04>...
    > kludge this idea
    > ---
    > // class to create a JLable and put the time of day into it (refresh every
    > 1 second)
    > static class ClockLabel extends JLabel implements ActionListener
    > {
    > SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
    > ClockLabel(String text, int horizontalAlignment)
    > {
    > super(text, horizontalAlignment);
    > Timer t = new Timer(1000, this);
    > t.start();
    > }
    > public void actionPerformed(ActionEvent e)
    > {
    > setText((formatter.format(new Date())).toString());
    > }
    > }


    That's javax.swing.timer . I was refering to java.util.timer , but
    thanks for trying.

    Still need help,
    iksrazal
     
    iksrazal, Jul 1, 2004
    #3
  4. iksrazal

    iksrazal Guest

    (iksrazal) wrote in message news:<>...

    This damn near killed me, but as usual the solution was simple. Thanks
    to a nice fellow over at java ranch, this is how it turned out (just
    in case someone else has the same problem someday):

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

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

    public class PoolTest
    {
    class TimeOutTask extends TimerTask
    {
    Thread t;

    TimeOutTask(Thread t)
    {
    this.t = t;
    }

    public void run()
    {
    if(t!= null && t.isAlive())
    {
    t.interrupt();
    }
    }
    }

    class MyRunnable implements Runnable
    {
    //set as true to be a daemon thread and therefore exit on
    interrupt
    Timer timer = new Timer(true);

    public void run()
    {
    timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
    try
    {
    System.out.println("MyRunnable...");
    Thread.sleep(10000);
    }
    catch (InterruptedException ie)
    {
    System.out.println("MyRunnable error...");
    ie.printStackTrace();
    }
    }
    }

    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();
    }
    }
    }

    iksrazal
     
    iksrazal, Jul 1, 2004
    #4
  5. iksrazal

    iksrazal Guest

    (iksrazal) wrote in message news:<>...

    This damn near killed me, but as usual the solution was simple. Thanks
    to a nice fellow over at java ranch, this is how it turned out (just
    in case someone else has the same problem someday):

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

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

    public class PoolTest
    {
    class TimeOutTask extends TimerTask
    {
    Thread t;

    TimeOutTask(Thread t)
    {
    this.t = t;
    }

    public void run()
    {
    if(t!= null && t.isAlive())
    {
    t.interrupt();
    }
    }
    }

    class MyRunnable implements Runnable
    {
    //set as true to be a daemon thread and therefore exit on
    interrupt
    Timer timer = new Timer(true);

    public void run()
    {
    timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
    try
    {
    System.out.println("MyRunnable...");
    Thread.sleep(10000);
    }
    catch (InterruptedException ie)
    {
    System.out.println("MyRunnable error...");
    ie.printStackTrace();
    }
    }
    }

    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();
    }
    }
    }

    iksrazal
     
    iksrazal, Jul 1, 2004
    #5
  6. iksrazal

    iksrazal Guest

    (iksrazal) wrote in message news:<>...

    This damn near killed me, but as usual the solution was simple. Thanks
    to a nice fellow over at java ranch, this is how it turned out (just
    in case someone else has the same problem someday):

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

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

    public class PoolTest
    {
    class TimeOutTask extends TimerTask
    {
    Thread t;

    TimeOutTask(Thread t)
    {
    this.t = t;
    }

    public void run()
    {
    if(t!= null && t.isAlive())
    {
    t.interrupt();
    }
    }
    }

    class MyRunnable implements Runnable
    {
    //set as true to be a daemon thread and therefore exit on
    interrupt
    Timer timer = new Timer(true);

    public void run()
    {
    timer.schedule(new TimeOutTask(Thread.currentThread()), 1000);
    try
    {
    System.out.println("MyRunnable...");
    Thread.sleep(10000);
    }
    catch (InterruptedException ie)
    {
    System.out.println("MyRunnable error...");
    ie.printStackTrace();
    }
    }
    }

    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();
    }
    }
    }

    iksrazal
     
    iksrazal, Jul 1, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John C. Bollinger
    Replies:
    0
    Views:
    471
    John C. Bollinger
    Aug 18, 2003
  2. Liz

    TimerTask priority

    Liz, Apr 17, 2004, in forum: Java
    Replies:
    3
    Views:
    3,061
    Roedy Green
    Apr 18, 2004
  3. Jeff

    TimerTask destruction

    Jeff, Aug 19, 2004, in forum: Java
    Replies:
    4
    Views:
    839
  4. tkonrath
    Replies:
    4
    Views:
    6,246
    Roedy Green
    Oct 17, 2005
  5. Recheis Meinrad
    Replies:
    0
    Views:
    99
    Recheis Meinrad
    Aug 4, 2003
Loading...

Share This Page