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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:08:43+00:00 2026-05-28T20:08:43+00:00

I created a stack class like this. Some times it runs and sometimes it

  • 0

I created a stack class like this. Some times it runs and sometimes it will through ArrayIndexOutofBoundException. What’s wrong in threading? Couldn’t understand please help.

public class StackImpl<E> implements Stack<E>{

private E[] stackArray;

private int topOfStack;

public StackImpl(int size) {
    stackArray = (E[]) new Object[size];
    topOfStack = -1;
}

public synchronized void push(E e) {
    while (isFull()) {
        try {
            System.out.println("stack is full cannot add " + e);
            wait();
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }
    }
    stackArray[++topOfStack] = e;
    System.out
            .println(Thread.currentThread() + " :notifying after pushing");
    notify();
}

public boolean isEmpty() {
    return topOfStack == 0;
}

public synchronized E pop() {
    while (isEmpty()) {
        try {
            System.out.println("stack is empty cannot pop ");
            wait();
        } catch (InterruptedException e) {

        }
    }
    System.out.println(topOfStack);
    E element = stackArray[topOfStack];
    stackArray[topOfStack--] = null;
    System.out.println(Thread.currentThread() + " notifying after popping");
    notify();
    return element;
}

public boolean isFull() {
    return topOfStack >= stackArray.length-1;
}

public static void main(String[] args) {
    final Stack<Integer> stack = new StackImpl<Integer>(10);
    (new Thread("Pusher") {
        public void run() {
            while (true) {
                stack.push(10);
            }
        }
    }).start();
    (new Thread("Popper") {
        public void run() {
            while (true) {
                stack.pop();
            }
        }
    }).start();
}

}
  • 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-28T20:08:44+00:00Added an answer on May 28, 2026 at 8:08 pm

    You set

    topOfStack = -1;
    

    in the constructor of StackImpl, yet your isEmpty method checks for 0:

    public boolean isEmpty() {
        return topOfStack == 0;
    }
    

    So the while loop will fall through on first pop, if the pusher hasn’t added any values yet:

        public synchronized E pop() {
            while (isEmpty()) {
                try {
                    System.out.println("stack is empty cannot pop ");
                    wait();
                } catch (InterruptedException e) {
    
                }
            }
    

    Set the topOfStack to 0 on the constructor and it should work.

    Edit: come to think of it, instead change the isEmpty -method to return topOfStack < 0;, and leave the topOfStack = -1; in your constructor… otherwise element in index 0 is never popped.

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

Sidebar

Related Questions

I've created my own ProfileUIViewController class that is a UINavigationControllerDelegate . I display this
I would like some clarification on what happens when a class is instantiated on
I have created a Treeview and used a stack panel to include a checkbox,
I have a button that is programatically created, it's content is a stack panel
this is my first question on stack overflow, so be gentle. Let me first
I have already created a windows service that monitors the network for some specific
I have an application that will record and play audio files. Some of the
Hello good Stack Overflow people, I do have a business problem and would like
I'm working with some Android code created by other developers, and they've asked me
I'd like to have some help on an issue I face. I'm using serialization

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.