Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this acceptable OO Design

Is this good OO Design assuming you want every inheriting class to be a infinite Thread ? Any better/more elegant way of doing similar thing?

public abstract class Base implements Runnable {

protected abstract void doSomething();

public void run() {

    while ( true ) {
        Thread.sleep(1000);
        doSomething();
    }
}
}
like image 553
noi.m Avatar asked Apr 01 '12 23:04

noi.m


1 Answers

If you only want doSomething to execute every second, you could move the task to its own Runnable and schedule it with a ScheduledExecutorService. This way you can reduce the number of threads in your program and save resources.

like image 164
Jeffrey Avatar answered Oct 08 '22 02:10

Jeffrey