0
import java.math.BigInteger;
import java.util.Arrays;

public class ThreadDemo {

    public static void main(String[] args) {

        int num_threads = 1000;
        MyThread myt[] = new MyThread[num_threads];
        for (int i = 0; i < num_threads; ++i) {
            myt[i].generatesequence(100);
            myt[i].start();
        }
        System.out.println("all threads spawned");

    }

}

class MyThread extends Thread {
    public int sum = 0;
    public int len = 0;

    MyThread(int n) {
        len = n;
    }

    @Override
    public void run() {
        sum = 0;
        for (int i = 1; i <= len; ++i) {
            sum += i;
        }
    }

    public void generatesequence(int n) {

        // Work out total number of combinations (2^n)
        BigInteger bitValue = BigInteger.valueOf(2).pow(n);
        int firstOne = n - 1;

        // For each combination...
        while (firstOne >= 0) {
            bitValue = bitValue.subtract(BigInteger.ONE);
            firstOne = bitValue.getLowestSetBit();

            // Initialise an array with 'n' elements all set to -1
            int[] resultForThisCombination = new int[n];
            Arrays.fill(resultForThisCombination, -1);

            if (firstOne >= 0) {
                // We now go through each bit in the combination...
                for (int bit = firstOne; bit < n; bit++) {
                    // If the bit is set, set array element to 1 else set it to -1...
                    if (bitValue.testBit(bit)) {
                        resultForThisCombination[bit] = 1;
                    }
                }
                System.out.println(Arrays.toString(resultForThisCombination));
            }

        }

    }
}

Need to create a thread for each configuration. Configuration have only 1 or -1 for sequence of 1 you get an array of [1] [-1] For sequence of 2 you get [1, 1] [-1, 1] [1, -1] [-1, -1] Need a thread for each row also the error message i am getting are the following Exception in thread "main" java.lang.NullPointerException at ThreadDemo.main(ThreadDemo.java:11)

  • You haven't initialized the cells of your `myt` array. So they are all `null`. So when you try to use one ... you get an NPE. – Stephen C Feb 17 '19 at 07:23

1 Answers1

0

Your Code is missing initialization of the thread, before you call the generateSequence() method on it. Add following before myt[i].generatesequence(100) statement:

    myt[i]= new MyThread(100);