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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:53:41+00:00 2026-05-22T14:53:41+00:00

I know that this is simple question but i’m somehow confused. If i understand

  • 0

I know that this is simple question but i’m somehow confused.

If i understand well, in simple words, when request arrive to web server, he creates thread for each request to some servlet.

Consider that we have next code in MyServlet(i left out exception handling and similar):

synchronized protected void doGet( ... ...){
    PrintWritet pw=response.getWriter();
    String param=request.getParameter("p");

    if(param.equals("a")){
        wait();
    }else{
        notifyAll();
    }

    pw.write("Hello!");
}

I expect that this servlet will stuck, because first thread (with param=a) that enters this method will hold on wait forever, because any other future thread will stuck in front of doGet because of synchronized keyword, and because of that notifyAll will never get executed.

Now, if i open new tab in browser and hit /MyServlet?p=a, browser Waiting for 127.0.0.1…
After that, i open new tab and hit /MyServlet?p=b (or anything that is !=a) first tab is released and print out “Hello!” message.

This means that second thread has entered in doGet, and executed notifyAll.

Why is this happening? What i missed?

  • 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-22T14:53:42+00:00Added an answer on May 22, 2026 at 2:53 pm

    Because wait() releases the lock it previously obtained by entering the synchronized block. From the javadoc for Object.wait:

    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 your first request obtains the lock, enters the doGet method, and calls wait (which releases the lock and waits). The second request obtains the lock, enters doGet, and calls notifyAll, which wakes up the first request’s thread.

    It’s critical that you read carefully the documentation for methods like wait and notify/notifyAll before you you use them, or you’ll get into trouble.

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

Sidebar

Related Questions

I know that this is supposedly a super simple question, but I've been struggling
I know this must be a simple question, but I know that in PHP
I know that this is a simple question for PHP guys but I don't
I know that this is a really simple question, but where should I place
I know this is going to be something simple that I'm just missing somehow,
I think that this is a relatively simple question. I just want to know
I'm sorry, I know this is a very simple question but I've search here
I know this is a simple question but difficult to formulate in one sentence
I know this is probably a simple question but, I have a ASP.NET WebSite
Ok I know that this is trivial question but: How can i remove lines

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.