Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:20:51+00:00 2026-06-12T14:20:51+00:00

ok. I need to make three threads: one to get odd numbers, one to

  • 0

ok. I need to make three threads: one to get odd numbers, one to get evens, and one to add the odd and evens together. The output would be something like this (1,2,3,3,4,7…).
I’m new to threads and still shaky on how they work but this is what I have so far:

class even extends Thread 
{
    public void 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 
{ 
    public void 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();

    } 
} 

This prints out 0,2,4…and then 1,3,5.
How to interleave? And is interleaving what I want and should I synchronize the threads as well?
What I don’t understand is how to get the values of the odd and even thread into a third to add the sum.
Apologies beforehand if I didn’t get the formatting correct for the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:20:52+00:00Added an answer on June 12, 2026 at 2:20 pm

    you need a way of “passing” the numbers from your producer threads (even & odd) to your “consumer” thread.

    ‘Simplest’ but not best way is a mutable array, with one entry — and you’ll have to synchronize/ lock on something (the array?) to avoid concurrent threads corrupting it.

    Perhaps the easier & better way, might be to use a ‘double-ended queue’ such as ArrayDeque. This lets values be put in at one end, and removed from the other. Again, access to it needs to be synchronized.

    Create the queues external to all threads, then pass them in/ or make them visible.

    class Example {
        protected Deque<Integer> queueEven = new ArrayDeque<Integer>();
        protected Deque<Integer> queueOdd = new ArrayDeque<Integer>();
        // you can synchronize on some mutable flag to finish, too.
    
        class Even extends Thread {
            public void run() {
                for (int i = 0; i < 10; i += 2) {
                    synchronized (queueEven) {
                        queueEven.add( i);
                    }
                }
            } 
        } 
    
        class Odd extends Thread {
            public void run() {
                for (int i = 1; i < 10; i += 2) {
                    synchronized (queueOdd) {
                        queueOdd.add( i);
                    }
                }
            } 
        } 
    
        class AddEvenOdd extends Thread {
            public void run() {
                 while (true) {
                     int even;
                     synchronized (queueEven) {
                         even = queueEven.removeFirst();
                     }
                     int odd;
                     synchronized (queueOdd) {
                         odd = queueOdd.removeFirst();
                     }
                     int result = even + odd;
                     System.out.println("result="+result);
                 }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a moderate system like the one in fmylife.com. Basically the
Here's my scenario: I need to make three or four calls to different WCF
I need to be able to make a tree like structure in the appengine
I need to make a administrative GUI with lots of functionality like lists, forms,
I need to make a program with a limited amount of threads (currently using
I need to make a few programs in C and i cannot get the
There is a weird code here that I need to make work.. can you
I need make some action (dump statistical data) before the Dart program ends. The
I need make all of my posts update. I use bulk upload for store,
Need to make certain Ruby strings in my program to be immutable. What is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.