简体   繁体   中英

Abstract class that implements Runnable into interface

How can I refactor an abstract class that implements Runnable into an interface?

import com.codahale.metrics.health.HealthCheck;

public abstract class ApplicationProcessor implements Runnable {

    public abstract HealthCheck getHealthCheck();
}

UPDATE: And also how can I remove the usage of a generic wildcard type for the future?

import io.dropwizard.lifecycle.Managed;
import java.util.concurrent.Future;
import com.codahale.metrics.health.HealthCheck;

public class ManagedBean extends HealthCheck implements Managed {

    private final String name;
    private final ManagedThreadPool threadPool;
    private final ApplicationProcessor processor;
    private Future<?> future; // HERE sonarqube complains..

    public ManagedBean( String name, ManagedThreadPool threadPool, ApplicationProcessor processor ) {
        this.name = name;
        this.threadPool = threadPool;
        this.processor = processor;
    }

    @Override
    public void start() throws Exception {
        future = threadPool.submit( processor );
    }

    @Override
    public void stop() throws Exception {
        if ( !future.isDone() ) {
            future.cancel( true );
        }
    }

    @Override
    protected Result check() throws Exception {
        return processor.getHealthCheck().execute();
    }

}

Thanks

public interface ApplicationProcessor extends Runnable {
    // interface methods are by default public and abstract
    HealthCheck getHealthCheck();
}

As for 2nd part use

Future<Void> or just Future

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM