import java.io.*; import java.util.*; public class ThreadingMain { public static void main(String[] args)throws InterruptedException { sumClass EvenThread = new sumClass("Even Thread"); sumClass OddThread = new sumClass("Odd Thread"); sumClass SumThread = new sumClass("Sum Thread"); EvenThread.start(); //OddThread.start(); //SumThread.start(); } } class sumClass extends Thread { private int EvenNumbers[] = new int[10]; private int OddNumbers[] = new int[10]; private int Sum[] = new int[10]; private String myName; public sumClass(String name) { myName = name; } public String getThreadName() { return myName; } private int[] getEvenNumbers() { for (int even = 0; even < 10; even++) { if(even%2 == 0) { for(int i = 0; i < 10; i++) { EvenNumbers[i] = even; break; } } } return EvenNumbers; } public int[] getOddNumbers() { for (int odd = 0; odd < 10; odd++) { if(odd%2 != 0) { for(int i = 0; i < OddNumbers.length; i++) { OddNumbers[i] = odd; break; } } } return OddNumbers; } public int[] Summing() { int[] Even = getEvenNumbers(); int[] Odd = getOddNumbers(); for(int i = 0; i < 10; i++) { Sum[i] = Even[i] + Odd[i]; } return Sum; } public void run() { int[] Evens = getEvenNumbers(); int[] Odds = getOddNumbers(); int[] Sums = Summing(); try { for(int i = 0; i < Evens.length; i++) { System.out.println(Evens[i]); break; } for(int i = 0; i < Odds.length; i++) { System.out.println(Odds[i]); break; } for(int i = 0; i < Sums.length; i++) { System.out.println(Sums[i]); break; } } catch(Exception e){} } }
4 Replies  717 Views  Last Post: 11 October 2012  11:50 AM
#1
cant get threads to output odd and even numbers...still
Posted 10 October 2012  06:48 PM
What am I doing wrong? I get 8,9, 17 as output. Three numbers
Replies To: cant get threads to output odd and even numbers...still
#2
Re: cant get threads to output odd and even numbers...still
Posted 10 October 2012  06:57 PM
Start each word in a class name with capital letters please.
The problem is with your inner for loops in the odd and even methods.
Every time you find a new even or odd number you are setting EVERY index of the array to that number. This means you are overwriting every index every time you are finding a new even or odd number, so the last even or odd you find will be equal to every index of the array.
In this case 0  9. 9 is the last odd. 8 is the last even. Since every index of even is 8 and every index of odd is 9. Every index of sum is 9+8 (17)
The problem is with your inner for loops in the odd and even methods.
for(int i = 0; i < 10; i++) { EvenNumbers[i] = even; break; }
Every time you find a new even or odd number you are setting EVERY index of the array to that number. This means you are overwriting every index every time you are finding a new even or odd number, so the last even or odd you find will be equal to every index of the array.
In this case 0  9. 9 is the last odd. 8 is the last even. Since every index of even is 8 and every index of odd is 9. Every index of sum is 9+8 (17)
#3
Re: cant get threads to output odd and even numbers...still
Posted 10 October 2012  07:10 PM
oh damn!
wait so how do i get the numbers into the array without overwriting?
wait so how do i get the numbers into the array without overwriting?
#4
Re: cant get threads to output odd and even numbers...still
Posted 10 October 2012  07:25 PM
There are 3 ways to do this. They are going to go in order of easiest to hardest.
1.Instead of arrays. Use a collection. (This isn't always a option and is actually probably just as difficult as option 2)
2.Keep a counter for the even array, and the odd array. Start the counter at 0, and every time you find a new odd or even use the counter as the index. Then add 1 to the counter.
3. Make a method that can return the next empty index in an array. Since you did not initialize your arrays the next empty value will be 0. Just loop through the array. Look for 0. When you find the first index that is equal to 0, return that index. That will be the index you can use to set your next value.
1.Instead of arrays. Use a collection. (This isn't always a option and is actually probably just as difficult as option 2)
2.Keep a counter for the even array, and the odd array. Start the counter at 0, and every time you find a new odd or even use the counter as the index. Then add 1 to the counter.
3. Make a method that can return the next empty index in an array. Since you did not initialize your arrays the next empty value will be 0. Just loop through the array. Look for 0. When you find the first index that is equal to 0, return that index. That will be the index you can use to set your next value.
#5
Re: cant get threads to output odd and even numbers...still
Posted 11 October 2012  11:50 AM
Another point to make is that the way you structured this, your threads are not actually talking to each other.
The reason you had to comment the start methods or your oddThread and sumThread objects is that every one of the three threads you created will fill the even array, then fill the odd array, then calculate the sum  uncommenting the start methods will actually run the whole sequence 3 times.
Some hints for you:
Hope this helps.
EDIT: Also note that the Odd and Even threads are doing the exact same thing, the only difference is that one of them starts from 0 and the other starts from 1
The reason you had to comment the start methods or your oddThread and sumThread objects is that every one of the three threads you created will fill the even array, then fill the odd array, then calculate the sum  uncommenting the start methods will actually run the whole sequence 3 times.
Some hints for you:
 You do not need arrays, you only need to print the sum of the latest odd and even numbers
 You will need 3 threads, 2 that count and 1 that sums
 You need to use "synchronized" code or some kind of Lock object
 Your counter threads should wait for sum to finish printing
 Your summing thread should wait for the counter to have a new number in some "static" variable
Hope this helps.
EDIT: Also note that the Odd and Even threads are doing the exact same thing, the only difference is that one of them starts from 0 and the other starts from 1
This post has been edited by rfs02: 11 October 2012  01:47 PM
Page 1 of 1
