Não é mais possível fazer postagens ou usar assinaturas novas da Usenet nos Grupos do Google. O conteúdo histórico continua disponível.
Dismiss

TimerTask inside Runnable?

5.582 visualizações
Pular para a primeira mensagem não lida

iksrazal

não lida,
29 de jun. de 2004, 14:30:1129/06/2004
para
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

Liz

não lida,
29 de jun. de 2004, 15:57:4829/06/2004
para

"iksrazal" <iksr...@terra.com.br> wrote in message
news:850ed936.04062...@posting.google.com...

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


iksrazal

não lida,
1 de jul. de 2004, 11:24:0201/07/2004
para
"Liz" <L...@nospam.com> 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

não lida,
1 de jul. de 2004, 18:02:2101/07/2004
para
iksr...@terra.com.br (iksrazal) wrote in message news:<850ed936.04062...@posting.google.com>...

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

não lida,
1 de jul. de 2004, 18:02:4801/07/2004
para
iksr...@terra.com.br (iksrazal) wrote in message news:<850ed936.04062...@posting.google.com>...

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

não lida,
1 de jul. de 2004, 18:03:1001/07/2004
para
iksr...@terra.com.br (iksrazal) wrote in message news:<850ed936.04062...@posting.google.com>...

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

contac...@gmail.com

não lida,
15 de fev. de 2016, 20:42:1715/02/2016
para
Hey iksrazal this is what im looking for Excellent for posting this . You really made my day.Thanks a Tonnnnnnn. Keep it up!

Thanks,
Kiran
0 nova mensagem