cant get threads to behave

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 672 Views - Last Post: 05 October 2012 - 10:26 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 behave

Posted 03 October 2012 - 11:37 PM

I have an assignment that wants three threads: One to count even numbers, one to count odd numbers and the third to add the numbers of the first and second thread together.
Im just starting threads and this is my first thread assignment.

I know there has to be a better way to have threads work with each other than using a different class for each thread, which is the way I coded my odd and even threads.

I'm not sure how to get the third thread to read the values of the first two. I tried a function that reads two threads and adds them together but that didn't work. I feel like this is much simpler than I'm making it but I can't figure it out

Thanks in advance.

class even extends Thread 
{

even()
{
Thread ThreadEven = new Thread(this); 
start(); 
} 
public void run() 
{
try
{
	for(int i = 0; i < 10; i += 2) 
	{
	System.out.println(i);
	}
	Thread.sleep(1000);
}
catch(Exception e) 
{
System.out.println("Error: Thread Interrupted");
} 
} 
}

class odd extends Thread 
{ 

odd() 
{
Thread ThreadOdd = new Thread(this); 
start(); 
} 
public void run() 
{
try
{
for(int i = 1;i < 10; i += 2) 
System.out.println(i);
Thread.sleep(1000);
}
catch(Exception e) 
{
System.out.println("Error: Thread Interrupted");
} 
} 
}
class ThreadEvenOdd
{
public static void main(String args []) 
	{
		even e = new even();
		odd o = new odd();
		
		} 
} 



Is This A Good Question/Topic? 0
  • +

Replies To: cant get threads to behave

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: cant get threads to behave

Posted 04 October 2012 - 03:46 AM

As you suggested, the multiple classes probably aren't necessary, but they're not a bad thing either. And, as always, there are multiple ways to do what you've described.

What does the assignment require? Must all three threads run at the same time, or can the counting threads run followed by the adding thread?

You might try programming the assignment without threads and then add the concurrency where it makes sense or is required.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: cant get threads to behave

Posted 04 October 2012 - 03:51 AM

View Posthealix, on 04 October 2012 - 02:37 AM, said:

I know there has to be a better way to have threads work with each other than using a different class for each thread, which is the way I coded my odd and even threads.

May be may be not you shouldn't worry about that. Why would that design be bad ?
However you should worry about learning how to indent code correctly :) You are a pain to read... I quit
Was This Post Helpful? -1
  • +
  • -

#4 healix  Icon User is offline

  • D.I.C Head

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

Re: cant get threads to behave

Posted 04 October 2012 - 07:09 AM

Quote

May be may be not you shouldn't worry about that. Why would that design be bad ?
However you should worry about learning how to indent code correctly :) You are a pain to read... I quit


These comments are the best. They have nothing to do with what I'm asking and somehow they always have a clever, little, childish insult in them.
Why respond at all if you're not gonna leave anything helpful?

Indenting code does not make the code run and damn you couldn't get through 50 lines of code? Thats something you should be worrying about.

But, hey, thanks for replying...I think. Can't wait for your next reply.

This post has been edited by healix: 04 October 2012 - 07:12 AM

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 behave

Posted 04 October 2012 - 08:33 AM

Can you clarify what your expected output should be?

From your code, it looks like you're expecting that the Even thread would print 2, 4, etc.. and the odd thread to print 1, 3, etc...

Are you expecting the third thread to print the numbers after the Even and Odd threads have printed them (1 1 2 2 3 3 etc...) or to print the sum of the numbers after they have been printed (1 2 1+2 3 4 3+4)?

Either way, it looks like you are trying to use the output of your Odd and Even threads to do something in your OddEven thread, and if that's the expectation, you should find a way for the OddEven thread to wait for data to be available (possibly use a Lock object).
Was This Post Helpful? 0
  • +
  • -

#6 healix  Icon User is offline

  • D.I.C Head

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

Re: cant get threads to behave

Posted 04 October 2012 - 05:12 PM

you're right rfs02. Im trying to use a third thread to sum up the values from my odd and even threads.

But right now the out put is 0,2,4...then 1,3,5...

The way you wrote it:(1 2 1+2 3 4 3+4) is what I'm trying to get.

Thread e outputs 0 thread o outputs 1 then the third thread will add them together and so on until 10.

I've been reading the API on locks and synchronization but what I can't figure out is how does the third thread access the values of the first two threads?
Was This Post Helpful? 0
  • +
  • -

#7 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: cant get threads to behave

Posted 04 October 2012 - 05:54 PM

OK, let's start from the beginning. It's fairly simple to create two threads to print even and odd numbers. You run them, but neveer know which Thread will run first. You tell us you're trying to sum numbers, but I see no variables. More details, please.

public static void main(String[] args) {
        Thread even = new Thread(){
            public void run() {
                for (int i = 0; i < 10; i += 2) {
                    System.out.println(i);
                }
            }
        };
        
        Thread odd = new Thread() {
            public void run() {
                for (int i = 0; i <= 10; i += 3) {
                    if (i == 0) {
                        System.out.println();
                    }
                    System.out.println(i);
                }
            }
        };
        even.start();
        odd.start();
    }


Was This Post Helpful? 0
  • +
  • -

#8 healix  Icon User is offline

  • D.I.C Head

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

Re: cant get threads to behave

Posted 04 October 2012 - 06:03 PM

Thats all the details I have. When I run this program I get an even and odd thread of numbers but I don't know how to access those values with the third thread.

If you're talking variables to hold the data of the threads and then sum them together that's not the way I want to do it.
Was This Post Helpful? 0
  • +
  • -

#9 Sir Papsby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: cant get threads to behave

Posted 04 October 2012 - 06:05 PM

do thread sum = new Thread(){

then set sum = odd + even;
Was This Post Helpful? 0
  • +
  • -

#10 healix  Icon User is offline

  • D.I.C Head

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

Re: cant get threads to behave

Posted 04 October 2012 - 06:12 PM

Quote

do thread sum = new Thread(){

then set sum = odd + even;


Was the first thing I tried.
Was This Post Helpful? 0
  • +
  • -

#11 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: cant get threads to behave

Posted 04 October 2012 - 06:32 PM

View PostSir Papsby, on 05 October 2012 - 01:05 AM, said:

do thread sum = new Thread(){

then set sum = odd + even;


lol :)

View Posthealix, on 05 October 2012 - 01:03 AM, said:

Thats all the details I have. When I run this program I get an even and odd thread of numbers but I don't know how to access those values with the third thread.

If you're talking variables to hold the data of the threads and then sum them together that's not the way I want to do it.


If you don't want to do it that way, then there is nothing to access, and nothing to add. All you're doing is print statements. Your goal, as you've described it, is impossible.

This post has been edited by farrell2k: 04 October 2012 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

#12 healix  Icon User is offline

  • D.I.C Head

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

Re: cant get threads to behave

Posted 04 October 2012 - 07:41 PM

ok so I need to store the values of the threads in variables? What variables? A variable for each thread? Then add the variables together? I thought threads worked differently. With variables it seems like a regular array.

By the way. If you haven't guessed. This is my first time using threads.
Was This Post Helpful? 0
  • +
  • -

#13 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: cant get threads to behave

Posted 04 October 2012 - 08:20 PM

You could store all even numbers in an int[], then all odd numbers in an in[t], then create a third thread that pulls each element from both arrays, adds them, then prints them.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: cant get threads to behave

Posted 04 October 2012 - 08:23 PM

lol)))
Why not ? :^:
Was This Post Helpful? 0
  • +
  • -

#15 healix  Icon User is offline

  • D.I.C Head

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

Re: cant get threads to behave

Posted 04 October 2012 - 10:26 PM

Quote

lol)))
Why not ? :^:


You came out of your hole to say that? Damn, man. Get a life.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2