Question
public class ThreadDemo { public static void main(String[] args) { int num_threads = 8 MyThread myt[] = new MyThread[num_threads]; for (int i = 0; i
public class ThreadDemo { public static void main(String[] args) { int num_threads = 8 MyThread myt[] = new MyThread[num_threads]; for (int i = 0; i < num_threads; ++i) { myt[i] = new MyThread(i+1); myt[i].generatesequence(2); myt[i].start(); } System.out.println("all threads spawned"); **// join the threads, this must be in a try-catch block** for (int i = 0; i < num_threads; ++i) { try { if (myt[i].isAlive()) { myt[i].join(); } } catch (Exception e) { } } ***// get results from the thread objects after all threads are done spawning*** for (int i = 0; i < num_threads; ++i) { int s = myt[i].sum; System.out.println("thread #, sum = " + i + " " + s); } System.out.println("end main"); } }
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)); } } } }
I am trying to generate individual thread for each configuration that only contains 1 or -1
for sequence 1
[-1] [1]
for sequence 2
[-1,-1] [1, -1] [-1,1] [1,1]
basically if i do it for configuration of 1 i will need two threads and for configuration 2 i will need 4 thread and for 3 need 8 thread I am not getting the right number of thread and My sum isn't coming out for each thread correctly
I have created 4 threads for configuration of 2 then ran the threads in for loop created new thread to generate the sequences. I don't think i am summing threads correctly When I run the program this is what i get
[1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] [1, 1] [-1, 1] [1, -1] all threads spawned thread #, sum = 0 1 thread #, sum = 1 3 thread #, sum = 2 6 thread #, sum = 3 10 thread #, sum = 4 15 thread #, sum = 5 21 thread #, sum = 6 28 thread #, sum = 7 36 end main***
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started