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 7679269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:50:21+00:00 2026-05-31T17:50:21+00:00

I have two very similar programs each trying to run two threads OddThread and

  • 0

I have two very similar programs each trying to run two threads OddThread and EvenThread and trying to print the odd and even numbers in sequence . While the first one works , the second one hangs . Can anyone please pinpoint the bug in the second program ?

The first one which works :

public class ThreadTest {

public static void main(String[] args) {
    System.out.println("Odd Even test");
    NumHolder objNumHolder = new NumHolder();
    Odd o1 = new Odd(objNumHolder, "Odd Number Thread");
    Even e1 = new Even(objNumHolder, "Even Number Thread");
    o1.start();
    e1.start();
}
}

class NumHolder {
private int intCurrNum;
private boolean isEven = false;

public synchronized void printOddNumber(String tname) {
    while (isEven == true){
        try {
            wait();
        }catch (InterruptedException e) {
        }
    }
    isEven = true;
    System.out.println("Thread Name="+tname + "===Number="+intCurrNum);
    intCurrNum += 1;
    notifyAll();
}

public synchronized void printEvenNumber(String tname) {
    while (isEven == false) {
        try {
            wait();
        } catch (InterruptedException e) {
        }
    }
    isEven = false;
    System.out.println("Thread Name="+tname + "===Number="+intCurrNum);
    intCurrNum += 1;
    notifyAll();
}
}

class Even extends Thread {
private NumHolder objNumHolder;

public Even(NumHolder p_objNumHolder, String name) {
    super(name);
    objNumHolder=p_objNumHolder;
}

public void run() {
    for (int i = 0; i < 10; i++) {
        objNumHolder.printEvenNumber(getName());
    }
}
}
class Odd extends Thread {
private NumHolder objNumHolder;

public Odd(NumHolder p_objNumHolder,String name) {
    super(name);
    objNumHolder = p_objNumHolder;
}

public void run() {
    for (int i = 0; i < 10; i++) {
        objNumHolder.printOddNumber(getName());
    }
}
}

The second code which hangs :

class PrintClass {
int intCurrNum;
private boolean isEven = false;

synchronized void printOdd(){
    while(isEven){
        try{
            wait();
        }catch(InterruptedException ie){
            System.out.println("Interrupted exception in printOdd()");
            ie.printStackTrace();
        }
        isEven = true;
        System.out.println("Thread Name="+Thread.currentThread().getName() + "===Number="+intCurrNum);
        intCurrNum += 1;
        notifyAll();
    }
}

synchronized void printEven(){
    while(!isEven){
        try{
            wait();
        }catch(InterruptedException ie){
            System.out.println("Interrupted exception in printEven()");
            ie.printStackTrace();
        }
        isEven = false;
        System.out.println("Thread Name="+Thread.currentThread().getName() + "===Number="+intCurrNum);
        intCurrNum += 1;
        notifyAll();
    }
}
}
class ThreadOdd extends Thread {
PrintClass pc = null;

ThreadOdd(PrintClass pc , String name){
    super(name);
    this.pc = pc;
}

public void run(){
    for (int i = 0; i < 10; i++) {
        pc.printOdd();
    }
}
}
class ThreadEven extends Thread {
PrintClass pc = null;

ThreadEven(PrintClass pc,String name){
    super(name);
    this.pc = pc;
}

public void run(){
    for (int i = 0; i < 10; i++) {
        pc.printEven();
    }
}
}
public class EvenOddPrintClass {
public static void main(String[] args){
    PrintClass pc = new PrintClass();
    Thread to = new ThreadOdd(pc,"ThreadOdd");
    Thread te = new ThreadEven(pc,"ThreadEven");
    to.start();
    te.start();
}
}

Thanks.

  • 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-05-31T17:50:23+00:00Added an answer on May 31, 2026 at 5:50 pm

    I suggest you run your code in the debugger and step through both threads. It’s very educational. You will see exactly where the error is.

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

Sidebar

Related Questions

I have two very similar programs, as follows. Program A: no problem when running,
I have two question to put forward: I was very interested, even intrigued by
I have two very similar specs for two very similar controller actions: VoteUp(int id)
I have two functions that are very similar and i'd like to make a
I have a script in PHP which retrieves two very similar files and performs
I have an issue where I have two pages that deal with very similar
I have two very similar tables in our database, and I need to write
I have two very similar .aspx pages. Both of them contain a DropDownList control.
I have two images that are very similar. So for example suppose I drew
I have two very similar methods: public IQueryable<User> Find(Func<User, bool> exp) { return db.Users.Where(exp);

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.