首页 \ 问答 \ 将Runnable实现到接口的抽象类(Abstract class that implements Runnable into interface)

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

}

谢谢


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


原文:https://stackoverflow.com/questions/35712338
更新时间:2024-02-16 06:02

最满意答案

注意 :我已经用非常小的一段数据测试过了。试试看,如果你卡住了,就让我知道。 我们会从那里拿走它。

假设我们的数据看起来像这样

在这里输入图像描述

现在我们运行这个代码

Sub Sample()
    Dim oSht As Worksheet
    Dim arr As Variant, FinalArr() As String
    Dim i As Long, j As Long, k As Long, LRow As Long

    Set oSht = ActiveSheet

    With oSht
        LRow = .Range("A" & .Rows.Count).End(xlUp).Row

        arr = .Range("A2:H" & LRow).Value

        i = Application.WorksheetFunction.CountA(.Range("G:H"))

        '~~> Defining the final output array
        ReDim Preserve FinalArr(1 To (LRow + i - 3), 1 To 6)

        k = 0
        For i = LBound(arr) To UBound(arr)
            k = k + 1
            FinalArr(k, 1) = arr(i, 1)
            FinalArr(k, 2) = arr(i, 2)
            FinalArr(k, 3) = arr(i, 3)
            FinalArr(k, 4) = arr(i, 4)
            FinalArr(k, 5) = arr(i, 5)
            If arr(i, 6) <> "" Then FinalArr(k, 6) = arr(i, 6)

            For j = 7 To 8
                If arr(i, j) <> "" Then
                    k = k + 1
                    FinalArr(k, 1) = arr(i, 1)
                    FinalArr(k, 2) = arr(i, 2)
                    FinalArr(k, 3) = arr(i, 3)
                    FinalArr(k, 4) = arr(i, 4)
                    FinalArr(k, 5) = arr(i, 5)
                    FinalArr(k, 6) = arr(i, j)
                End If
            Next j
        Next i

        .Rows("2:" & .Rows.Count).Clear

        .Range("A2").Resize(UBound(FinalArr), 6).Value = FinalArr
    End With
End Sub

产量

在这里输入图像描述


Note: I have tested this with very small piece of data.. Give it a try and if you are stuck then let me know. We will take it from there.

Let's say our data looks like this

enter image description here

Now we run this code

Sub Sample()
    Dim oSht As Worksheet
    Dim arr As Variant, FinalArr() As String
    Dim i As Long, j As Long, k As Long, LRow As Long

    Set oSht = ActiveSheet

    With oSht
        LRow = .Range("A" & .Rows.Count).End(xlUp).Row

        arr = .Range("A2:H" & LRow).Value

        i = Application.WorksheetFunction.CountA(.Range("G:H"))

        '~~> Defining the final output array
        ReDim Preserve FinalArr(1 To (LRow + i - 3), 1 To 6)

        k = 0
        For i = LBound(arr) To UBound(arr)
            k = k + 1
            FinalArr(k, 1) = arr(i, 1)
            FinalArr(k, 2) = arr(i, 2)
            FinalArr(k, 3) = arr(i, 3)
            FinalArr(k, 4) = arr(i, 4)
            FinalArr(k, 5) = arr(i, 5)
            If arr(i, 6) <> "" Then FinalArr(k, 6) = arr(i, 6)

            For j = 7 To 8
                If arr(i, j) <> "" Then
                    k = k + 1
                    FinalArr(k, 1) = arr(i, 1)
                    FinalArr(k, 2) = arr(i, 2)
                    FinalArr(k, 3) = arr(i, 3)
                    FinalArr(k, 4) = arr(i, 4)
                    FinalArr(k, 5) = arr(i, 5)
                    FinalArr(k, 6) = arr(i, j)
                End If
            Next j
        Next i

        .Rows("2:" & .Rows.Count).Clear

        .Range("A2").Resize(UBound(FinalArr), 6).Value = FinalArr
    End With
End Sub

Output

enter image description here

相关问答

更多
  • 有一些限制(进一步来自John引用的Loren块): 您的代码必须在函数内运行 您必须没有“数据”的其他别名 “别名”的事情既重要又可能很难做到。 MATLAB使用copy-on-write,这意味着当您调用函数时,您传递的参数不会立即重复,但如果您在函数中修改它,则可能会复制它。 例如,考虑一下 x = rand(100); y = myfcn(x); % with myfcn.m containing: function out = myfcn(in) in(1) = 3; out = in * ...
  • 注意 :我已经用非常小的一段数据测试过了。试试看,如果你卡住了,就让我知道。 我们会从那里拿走它。 假设我们的数据看起来像这样 现在我们运行这个代码 Sub Sample() Dim oSht As Worksheet Dim arr As Variant, FinalArr() As String Dim i As Long, j As Long, k As Long, LRow As Long Set oSht = ActiveSheet With oSht ...
  • 如果重启Matlab不是一个选项,“打包”功能应该有帮助。 否则你也可以在没有gui的情况下使用matlab,并为每个文件编写一个启动和matlab的shell脚本。 If restarting Matlab is not an option, the "pack" function should help. Otherwise you could also use matlab without the gui and write a shell script that starts and matlab ...
  • 正如您的帖子上的评论所示,此错误来自工作记忆的短缺。 每个Variant类型变量占用16个字节,这就是您的代码需要大量内存的原因。 因此,解决此问题的一种方法是增加计算机上的物理内存。 其他解决方案是按一定数量的行过滤数据。 Sub ProcessRows() Dim originalData() As Variant Dim maxRow as Long, currentRow as Long, incrementRow maxRow = ActiveSheet.Usedrang ...
  • 我使用的解决方案是将数据帧缩小: df = pd.read_hdf('madre_merge_sort32.h5') for i,d in enumerate(np.array_split(df, 10)): d.to_pickle(str(i)+".p") 然后我跑过那些腌制的mini-dfs并计算每个中的rmse: for fn in glob.glob("*.p"): # process df values df = pd.read_pickle(fn) df.re ...
  • 即使在x64上,cPython 2.4也可能存在大内存分配问题: $ python2.4 -c "'a' * (2**31-1)" Traceback (most recent call last): File "", line 1, in ? MemoryError $ python2.5 -c "'a' * (2**31-1)" $ 更新到最近的python解释器(如cPython 2.7)以解决这些问题,并确保安装64位版本的解释器。 如果字符串具有非平凡的大小(即长于示例中的< ...
  • 除非分配给变量,否则不要使用圆括号。 Workbooks(theFile).SaveAs "Z:\test\vhb\" & newName, xlCSV You do not use parentheses unless you are assigning to a variable. Workbooks(theFile).SaveAs "Z:\test\vhb\" & newName, xlCSV
  • 在我的头顶 - 我看到你没有使用ARC。 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:requestURL] 你在某处发布这个操作吗? [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (networ ...
  • 所以它需要我的64位Linux(32GB)内存,略低于2GB。 In [5]: def f(): df = DataFrame(np.random.randn(50000,300)) df.stack().reset_index(1) In [6]: %memit f() maximum of 1: 1791.054688 MB per loop 既然你没有指定。 这根本不适用于32位(因为你通常不能分配一个2GB的连续块),但是如果你有合理的交换/内存应该可以工作。 So ...
  • 您使用的是什么版本的Scipy? 在我正在运行的版本(0.12.0)中,没有out参数,因为有两个输出参数: distances和indices ,它们都可以用于输出。 如果提供了这些并且是ndarray或子类的实例,scipy将使用它们进行转换。 从文档: 距离:ndarray,可选 用于距离数组的输出,必须是float64类型。 指数:ndarray,可选 用于索引的输出,必须是int32类型。 请注意,文档中存在拼写错误,至少在0.12.0中: distance应该是distances 。 不幸的是, ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。