Home > database >  New thread per each boolean true combination [closed]
New thread per each boolean true combination [closed]

Time:09-25

I have a doubt about what would be the better way to achieve this:

I have a Class that contains 5 boolean variables (example: 11111, being one combination). Now, the thing is that I'm gonna start a new thread per each true combination, meaning that if i have 11000 i want to start a thread for 11000, another for 10000 and the last one for 01000.

I have to know all the values of the input combinations. The most obvious and inefficient way would be just comparing all the 32 (in this case) combinations (00000, 00001, etc.) and starting only when and AND of that and the actual value (11000) is != 0, that would be the cases that i previous mentioned (11000, 10000 and 01000).

In that case I'm gonna have to do 32 comparisons every single time. The thing is that if then i have 6 booleans now i have to do 64, and so on.

Anyone can think of a better strategy to "capture" every combination?

CodePudding user response:

I think the only way to do it faster is through pre-calculation.

So, String[][] arr; and for (String a : arr[5]) {/* thread creation etc */} should be used.

Where '5' it is '00101'.

Also might helps: Integer.toBinaryString(5) and (int) Long.parseLong("00101", 2).

  • Related