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

  • Home
  • SEARCH
  • 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 8737075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:26:07+00:00 2026-06-13T10:26:07+00:00

Wrote a multi-thread program to print out odd and even numbers in sequence untill

  • 0

Wrote a multi-thread program to print out odd and even numbers in sequence untill the sequence reached 30.

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class NumPrintTX
{
public static void main(String[] args)
{
    final int max = 31;
    final AtomicInteger i = new AtomicInteger(0);
    Executor dd = Executors.newFixedThreadPool(2);

    final Object lock = new Object();

    dd.execute(new Runnable()
    {
        @Override
        public void run()
        {
            while (i.get() < max)
            {
                if (i.get() % 2 == 0)
                {
                    System.out.print(" " + i.getAndAdd(1));

                    synchronized(lock)
                    {
                        lock.notify();
                    }
                }
                else
                {
                    synchronized(lock)
                    {
                        try
                        {
                            lock.wait();
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    });
    dd.execute(new Runnable()
    {
        @Override
        public void run()
        {
            while (i.get() < max)
            {
                if (i.get() % 2 != 0)
                {
                    System.out.print(" " + i.getAndAdd(1));

                    synchronized(lock)
                    {
                        lock.notify();
                    }
                }
                else
                {
                    synchronized(lock)
                    {
                        try
                        {
                            lock.wait();
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    });
    do
    {
        try 
        {
            Thread.currentThread().sleep(1000);
            }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    while (i.get() != max);
}
}

when the program runs, it comes out just fine, but it won’t go to the next line so i could enter another command for whatever i want to do next. Any ideas why this is and what i can do to fix it?

modified code:
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class NumPrintTX
{
public static void main(String[] args)
{
    final int max = 31;
    final AtomicInteger i = new AtomicInteger(0);
    Executor dd = Executors.newFixedThreadPool(2);

    final Object lock = new Object();

    dd.execute(new Runnable()
    {
        @Override
        public void run()
        {
            while (i.get() < max)
            {
                if (i.get() % 2 == 0)
                {
                    System.out.print(" " + i.getAndAdd(1));

                    synchronized(lock)
                    {
                        lock.notify();
                    }
                }
                else
                {
                    synchronized(lock)
                    {
                        try
                        {
                            lock.wait();
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    });
    dd.execute(new Runnable()
    {
        @Override
        public void run()
        {
            while (i.get() < max)
            {
                if (i.get() % 2 != 0)
                {
                    System.out.print(" " + i.getAndAdd(1));

                    synchronized(lock)
                    {
                        lock.notify();
                    }
                }
                else
                {
                    synchronized(lock)
                    {
                        try
                        {
                            lock.wait();
                        }
                        catch (InterruptedException e)
                        {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    });
    do
    {
        try 
        {
            Thread.currentThread().sleep(1000);
            }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    while (i.get() != max);
}
public void close()
{
System.exit(0);
}
}
  • 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-13T10:26:08+00:00Added an answer on June 13, 2026 at 10:26 am

    You’re not stopping your thread pool, so it’s not letting your main program end.

    Consider the following code:

    import java.util.concurrent.Executors;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.atomic.AtomicInteger;
    
    class Worker implements Runnable {
        private AtomicInteger i;
        private Object lock;
        private int max;
        private int mod;
    
        Worker(AtomicInteger i_, Object lock_, int max_, int mod_){ 
            this.i   = i_;
            this.lock = lock_;
            this.max  = max_;
            this.mod  = mod_; 
        }
    
        @Override
        public void run(){
            while (i.get() < max)
            {
                if(i.get() % 2 == mod)
                {
                    System.out.print(" " + i.getAndAdd(1));
    
                    synchronized(lock){ lock.notify(); }
                }
                else
                {
                    synchronized(lock)
                    {
                        try { lock.wait(); }
                        catch (InterruptedException e) { e.printStackTrace(); }
                    }
                }
            }
        }
    }
    
    public class NumPrintTX
    {
       public static void main(String[] args)
        {
            final int max = 31;
            final AtomicInteger i = new AtomicInteger(0);
            ExecutorService dd = Executors.newFixedThreadPool(2);
    
            final Object lock = new Object();
    
            dd.execute(new Worker(i, lock, max, 0));
            dd.execute(new Worker(i, lock, max, 1));
    
            dd.shutdown();
        }    
    }
    

    Without the dd.shutdown(), your program will hang despite getting to the end.

    The main do-while loop and System.exit(0) will work but seems a bit “brute-force”.

    Also, this code streamlines your thread code and makes it much more readable without changing the underlying logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a multi-threaded program which does some CPU heavy computation with a lot
My question pertains to multi-threading in Java. I'm translating an app I wrote in
I need a cross-platform solution for multi-thread to write to a same file concurrently
I wrote multi-threaded FTP uploader on C#.NET using libcurl.NET. Everything works fine on my
If I write a multi-threaded java application, will the JVM take care of utilizing
I needed to write a script to enter multi-line input to a program (
I wrote a multi-threaded app to benchmark the speed of running LOCK CMPXCHG (x86
I currently wrote my program to use 32 threads and read 1 file per
Latelly I've been working with multi-thread coding, after a while writing I realized that
I have an existing multi-threaded C++ program which uses a pool of sockets for

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.