[英]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.