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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:02:12+00:00 2026-05-28T06:02:12+00:00

I recently had a problem with two threads sticking in deadlock because they weren’t

  • 0

I recently had a problem with two threads sticking in deadlock because they weren’t monitoring the same object the way I thought they were. As it turned out, implementing the Singleton pattern solved the problem. But why?

I only instantiated one instance of the class of which the object was a private property, so I expected it to be effectively singleton anyway.


For the sake of completeness of the question, here is also some code illustrating the difference:

Before the Singleton pattern was implemented:

class Worker {
    private BlockingQueue q = new LinkedBlockingQueue();

    public void consume(String s) {
        // Called by thread 1.
        // Waits until there is anything in the queue, then consumes it
    }

    public void produce(String s) {
        // Called by thread 2.
        // Puts an object in the queue.
    }

    // Actually implements Runnable, so there's a run() method here too...
}

The threads were started like this:

Worker w = new Worker();
new Thread(w).start();

// Producer also implements Runnable. It calls produce on its worker.
Producer p = new Producer(w);
new Thread(p).start();

Now, when I examined the queues that were actually used in produce() and consume(), System.identityHashCode(q) gave different results in the different threads.

With the singleton pattern:

class Worker {
    private static BlockingQueue q;
    private BlockingQueue getQueue() {
        if(q == null) {
            q = new LinkedBlockingQueue();
        }
        return q;
    }
    // The rest is unchanged...
}

Suddenly, it works. Why is this pattern necessary here?

  • 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-28T06:02:13+00:00Added an answer on May 28, 2026 at 6:02 am

    The problem is that you are creating a new Worker() inside the Server constructor. You have this:

    public Server(Worker worker) {
        this.clients = new ArrayList<ClientHandle>();
        this.worker = new Worker();  // This is the problem.
    
    
    // Don't do this in the Server constructor.
    this.worker = new Worker();
    
    // Instead do this:
    this.worker = worker;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had a problem with three separate servers running the same code all
I recently had a problem during the deployment of a windows service. Four computers
I recently had a problem in my app where some of the subviews I
Recently I had a problem in a costumer's computer. Our installer would hang during
I had recently a problem with oom-killer starting to kill processes after some time.
Recently two users of our software from the same company started experiencing random closures
I recently found a situation in which I had two related variables that had
Recently I had a problem using (pipe |-) when I wanted to communicate between
I recently had a problem in Emacs where I didn't know the syntax of
Recently I had a problem. How to disable scrolling in a particular area of

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.