8

I'm trying to run a Thread class every second. I cant use Runnable. I tried in the following way, but its throwing StackOverflowException. Can anyone please let me know a standard method to make a thread class run every second.

public class A extends Thread {

    public void run() {
       //do my stuff
      sleep(1*1000,0);
      run();
    }
}
Anoop
  • 23,044
  • 10
  • 62
  • 76
madhu
  • 744
  • 3
  • 8
  • 19
  • this link will help [how-do-you-use-a-timertask-to-run-a-thread][1] [1]: http://stackoverflow.com/questions/10029831/how-do-you-use-a-timertask-to-run-a-thread – Palejandro Jun 01 '13 at 12:02

3 Answers3

18

Use Timer's schedule() or scheduleAtFixedRate() (difference between these two) with TimerTask in the first argument, in which you are overriding the run() method.

Example:

Timer timer = new Timer();
timer.schedule(new TimerTask()
{
    @Override
    public void run()
    {
        // TODO do your thing
    }
}, 0, 1000);

Your example causes stack overflow, because it's infinite recursion, you are always calling run() from run().

Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
3

Maybe you want to consider an alternative like ScheduledExecutorService

ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);

/*This schedules a runnable task every second*/
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
  public void run() {
    DoWhateverYouWant();
  }
}, 0, 1, TimeUnit.SECONDS);
Christian Rapp
  • 1,853
  • 24
  • 37
  • Cant we make a thread run every second?? I could have considered Runnable or ScheduledExecutorService, but i'm stuck in a scenario where I must use a thread. Please help. – madhu Jun 01 '13 at 12:03
  • @madhu : **"i'm stuck in a scenario where I must use a thread."** - Why? Explain the reason why you can't use other ways to do what you need to do. Also, explain what it is you need to do. – Squonk Jun 01 '13 at 12:06
  • What if the task is taking long time and all the 5 threads are consumed? This won't work perfectly. See my answer :) – Eng.Fouad Jun 01 '13 at 12:19
  • @sqonk: I'm calling this Thread from mainActivity. This Thread is an abstract class which is extended by 3 more classes. I've called an abstract method in Void Run() which are defined in these 3 classes. Intention is that, once this thread gets started, rest of the classes gets executed simultaneously. – madhu Jun 01 '13 at 12:24
1
final ExecutorService es = Executors.newCachedThreadPool();
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new Runnable()
{
    @Override
    public void run()
    {
        es.submit(new Runnable()
        {
            @Override
            public void run()
            {
                // do your work here
            }
        });
    }
}, 0, 1, TimeUnit.SECONDS);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • Can we use this inside a Class which extends Thread?? Or should I've to create a service class?? – madhu Jun 01 '13 at 12:17
  • I tried to implement this method inside a Thread class. I was not working. I tried to debug and the flow was not getting entered into void run after scheduleAtFixedRate(new Runnable(). – madhu Jun 01 '13 at 13:22