4 Replies - 566 Views - Last Post: 11 October 2012 - 11:50 AM Rate Topic: -----

#1 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

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


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){}
	 
}
}





	
			

	
	




Is This A Good Question/Topic? 0
  • +

Replies To: cant get threads to output odd and even numbers...still

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

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.

 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)
Was This Post Helpful? 2
  • +
  • -

#3 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

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?
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

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.
Was This Post Helpful? 0
  • +
  • -

#5 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

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:

  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1