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();
}
}

New Topic/Question
Reply




MultiQuote





|