繁体   English   中英

将Runnable实现到接口的抽象类

[英]Abstract class that implements Runnable into interface

如何重构将Runnable实现到接口的抽象类?

import com.codahale.metrics.health.HealthCheck;

public abstract class ApplicationProcessor implements Runnable {

    public abstract HealthCheck getHealthCheck();
}

更新:以及将来如何删除通用通配符类型的使用?

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

}

谢谢

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

第二部分使用

Future<Void> or just Future

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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