Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads; Creating a separate thread to periodically do something

As an addition to my current application, I need to create a separate thread which will periodically do some processing

I've create a new class to do all this, and this class will be loaded on startup of my application.

This is what I have so far :

public class PeriodicChecker extends Thread
{
    static
    {
        Thread t = new Thread(new PeriodicChecker());
        while(true)
        {
            t.run();
            try
            {
                Thread.sleep(5000l);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * Private constructor to prevent instantiation
     */
    private PeriodicChecker()
    {

    }

    @Override
    public void run()
    {
        System.out.println("Thread is doing something");
        // Actual business logic here, that is repeated
    }

}

I want to make constructor private to prevent other people from attempting to instantiate this class accidentally. How can I achieve this?

Also, is there anything bad about my implementation of such requirements? I'm only creating one thread which will run then sleep, have I missed anything obvious? I haven't worked with threads before

like image 307
Jimmy Avatar asked Nov 03 '10 11:11

Jimmy


2 Answers

Java offers ScheduledExecutorService to schedule and run periodic tasks or tasks with delay. It should provide all the features you need. Timer is another class that offers similar functionalities, but I would recommend the ScheduledExecutorService over Timer for its flexibility of configuration and better error management.

like image 77
sjlee Avatar answered Nov 06 '22 05:11

sjlee


You have some conceptual erros in your code... for example:

  • You should call start() and not run(), because you are running the method sequentially and not simultaneously.
  • You can call start() only once, not once in each loop iteration. After that, the thread is in state TERMINATED, you should create a new thread to run it again
  • You should not create the thread in the static block, it is a bad practice, and maybe the Thread is running before you want it to run.

You should read some examples about thread, it is a little difficult to unserstand at the beginning, and you can have undesired effects very easily.

Here is a little example, that may do something similar to that you want:

public class PeriodicChecker extends Thread
{
    @Override
    public void run()
    {
        while(true) {
           System.out.println("Thread is doing something");
           Thread.sleep(5000);
        }
    }

}

public OtherClass {
   public static void main(String args[]) {
      Thread t = new PeriodicChecker();
      t.start();
   }
}

If you want that none can create a new Thread, you could create a singleton, so you will be sure that none is creating more threads.

like image 34
greuze Avatar answered Nov 06 '22 05:11

greuze