繁体   English   中英

Java,将值传递给可填充Callable类的构造函数

[英]Java, Passing value to a constructor of class inplementing Callable

因此,我实现了以下简单类以并行化独立的加密过程:

public class SelectionEncryptor implements Callable<BigInteger> {

    private DamgardJurik dj;
    private byte c;
    private int s;

    public SelectionEncryptor(DamgardJurik dj, byte c, int s) {
        this.dj = dj;
        this.c = c;
        this.s = s;
    }

    @Override
    public BigInteger call() throws Exception {

        dj.setS(s);

        BigInteger r = dj.encryption(BigInteger.valueOf(c));
        System.out.println("dj s: " + dj.s + ", given s: " + s + ", c: " + c); 

        return r;
    }

}

s只是我们使用的密码系统的一个参数,但它是确定加密深度的重要参数。

我初始化并运行线程,如下所示:

int s = s_max;

ExecutorService executor = Executors.newFixedThreadPool(selectionBits.length);
ArrayList<Future<BigInteger>> list = new ArrayList<Future<BigInteger>>();

// selectionBits is just a byte array holding 1's and 0's
for (int i = 0; i < selectionBits.length; i++) {

    dj.setS(s);

    SelectionEncryptor se = new SelectionEncryptor(dj, selectionBits[i], s);
    System.out.println("submitted: " + selectionBits[i] + " with s " + s + ", dj s: " + dj.s);

    Future<BigInteger> result = executor.submit(se);
    list.add(result);

    s--;
}

// collecting results

for (Future<BigInteger> future : list) {
    try {
        BigInteger f = future.get();
        out.println(f);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}

executor.shutdown();

问题是,即使我将正确的s值发送到SelectionEncryptor,它也将为其使用不同的值。 这是我的程序的示例输出:

submitted: 1 with s 6, dj s: 6
submitted: 0 with s 5, dj s: 6
submitted: 1 with s 4, dj s: 5
submitted: 0 with s 3, dj s: 3
submitted: 1 with s 2, dj s: 2
submitted: 0 with s 1, dj s: 1
dj s: 4, given s: 1, c: 0
dj s: 2, given s: 2, c: 1
dj s: 2, given s: 3, c: 0
dj s: 2, given s: 4, c: 1
dj s: 2, given s: 6, c: 1
dj s: 2, given s: 5, c: 0

我已经尝试仅在主函数中,仅在可调用类中设置s ,现在为了安全起见在两个函数中都设置了s ,但它们都没有起作用。

这个问题的根源是什么? 这些可调用实例是否共享DamgardJurick对象? 我想念的是什么?

抱歉,这个问题很长,我找不到简化它的方法。

您的所有线程共享相同的DamgardJurik实例,每个线程将其s设置为从主线程获得的值。 为每个线程使用一个单独的DamgardJurik实例。

暂无
暂无

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

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