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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:16:54+00:00 2026-05-19T22:16:54+00:00

The purpose of the following code is to implement a LIFO container just like

  • 0

The purpose of the following code is to implement a LIFO container just like a Stack, while, when retrieve an element, it will check if there is any existing element in the list, or it hold on thread that retrieving element until there is a new element get inserted.

public class Stack {

LinkedList list = new LinkedList();

public synchronized void push(Object x) {
    synchronized (list) {
        list.addLast(x);
        notify();
    }
}

public synchronized Object pop() throws Exception {
    synchronized (list) {
        if (list.size() <= 0) {
            wait();
        }
        return list.removeLast();
    }
}  

}

but deadlock may occur whenever call pop() where there is no element in the List. How to amend this Class to achieve the initial purpose while avoiding pontential deadlock. 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-19T22:16:54+00:00Added an answer on May 19, 2026 at 10:16 pm

    There are so many things I would change about this class its hard to know where to start. This simplest solution is to remove the synchronized(list) blocks as these are redundant and just cause problems as you can see.

    Another solution would be to use the Stack class which is builtin to Java. Your class could be confused for the built one as it has the same name.

    Or I would use a LinkedBlockingDeque which is thread safe as my collection and has pop() and push() methods.

    EDIT: If you want to know why you are getting a dead lock, it is because you are holding two locks (one on the Stack, the other on the LinkedList) but you are releasing only one lock with your wait() (the one on the Stack) meaning no other thread can get the lock on the list. There is no way to wait() on two objects at once.

    Your code is much the same as this (which might be clearer)

    public void push(Object x) {
      synchronized(this) {
        synchronized (list) { // cannot get a lock on `list` while pop() is called.
            list.addLast(x);
            this.notify();
        }
      }
    }
    
    public Object pop() throws Exception {
      synchronized(this) {
        synchronized (list) {
            if (list.size() <= 0) {
                this.wait(); // releases the lock on `this`, but keep the lock on `list`
            }
            return list.removeLast();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One recommends me the following code apparently only in .zshrc without explaining its purpose
I am not sure about the exact purpose of the following Rampion's code .
I've got following (simplified for example purpose) code and it works: void log(const string
The purpose of the following code is that when the user is holding the
What is the main purpose of the following JavaScript code? <script> var QunarUtil=new function(){var
I try to implement the following code: <a href = http://www.google.com/ id=external>go to google</a>
I try to implement the following code: <a href = http://www.google.com/ id=external>go to google</a>
The purpose of this code is to pull upgrade.zip from a central server, extract
The purpose is to use C++ in a useful way. I have just started
The question of today is the following: I'm developing a code generator for my

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.