multithreading - Java, Passing value to a constructor of class inplementing Callable -
so, have implemented following simple class parallelize independent encryption processes:
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 parameter of cryptosystem use, important 1 determining depth of encryption.
i initialize , run threads following:
int s = s_max; executorservice executor = executors.newfixedthreadpool(selectionbits.length); arraylist<future<biginteger>> list = new arraylist<future<biginteger>>(); // selectionbits byte array holding 1's , 0's (int = 0; < selectionbits.length; i++) { dj.sets(s); selectionencryptor se = new selectionencryptor(dj, selectionbits[i], s); system.out.println("submitted: " + selectionbits[i] + " s " + s + ", dj s: " + dj.s); future<biginteger> result = executor.submit(se); list.add(result); s--; } // collecting results (future<biginteger> future : list) { try { biginteger f = future.get(); out.println(f); } catch (interruptedexception | executionexception e) { e.printstacktrace(); } } executor.shutdown();
the problem is, if send correct s values selectionencryptor, uses different values it. example output of program:
submitted: 1 s 6, dj s: 6 submitted: 0 s 5, dj s: 6 submitted: 1 s 4, dj s: 5 submitted: 0 s 3, dj s: 3 submitted: 1 s 2, dj s: 2 submitted: 0 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
i have tried setting s in main function, in callable class , i'm setting in both of them safe none of them have worked yet.
what can source of problem? callable instances share damgardjurick object? i'm missing?
sorry long question, couldn't find way make simpler.
all threads share same instance of damgardjurik
, , each thread sets s
value got main thread. use separate instance of damgardjurik
each thread.
Comments
Post a Comment