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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:48:54+00:00 2026-05-14T21:48:54+00:00

I would appreciate your help in understanding a "Concurrency Example" from: http://forums.sun.com/thread.jspa?threadID=735386 public synchronized

  • 0

I would appreciate your help in understanding a "Concurrency Example" from:
http://forums.sun.com/thread.jspa?threadID=735386

public synchronized void enqueue(T obj) {
   // do addition to internal list and then...
   this.notify();
}

public synchronized T dequeue() {
   while (this.size()==0) {
       this.wait(); 
   }
   return // something from the queue
}

My Question is: Why is this code valid?

=> When I synchronize a method like "public synchronized" => then I synchronize on the "Instance of the Object ==> this".
However in the example above:

  1. Calling "dequeue" I will get the "lock/monitor" on this

  2. Now I am in the dequeue method. As the list is zero, the calling thread will be "waited"

  3. From my understanding I have now a deadlock situation, as I will have no chance of ever enqueuing an object (from another thread), as the "dequeue" method is not yet finished and the dequeue "method" holds the lock on this: So I will never ever get the possibility to call "enqueue" as I will not get the "this" lock.

Background: I have exactly the same problem: I have some kind of connection pool (List of Connections) and need to block if all connections are checked. What is the correct way to synchronize the List to block, if size exceeds a limit or is zero?

  • 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-14T21:48:55+00:00Added an answer on May 14, 2026 at 9:48 pm

    See: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait()

    The current thread must own this object’s monitor. The thread releases
    ownership of this monitor and waits until another thread notifies
    threads waiting on this object’s monitor to wake up either through a
    call to the notify method or the notifyAll method. The thread then
    waits until it can re-obtain ownership of the monitor and resumes
    execution.

    So no, there is no deadlock. When wait() is called, the monitor held by the thread is released, allowing enqueue (and other operations that are synchronized on this object) to be called on another thread. When the thread is notified, it will try to obtain the monitor again before continuing.

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

Sidebar

Related Questions

I'm new to regular expressions and would appreciate your help. I'm trying to put
I have an interesting problem and would appreciate your thoughts for the best solution.
I would really appreciate any suggestions, no matter how simple or complex, to help
Assuming such a query exists, I would greatly appreciate the help. I'm trying to
I would appreciate some guidance on modelling services and operations in WCF. I have
I've hit a blank here and would appreciate a kick start. I have two
I have a design issue that I would appreciate some input on. I would
I've been researching memory mapped files for a project and would appreciate any thoughts
My code works (yeah!) which sends json to a server.. would appreciate any thoughts
Any other tweaks for making emacs as vim-like as possible would be appreciated as

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.