noi.m
noi.m

Reputation: 3132

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

Upvotes: 5

Views: 193

Answers (1)

Jeffrey
Jeffrey

Reputation: 44808

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.

Upvotes: 11

Related Questions