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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:57:53+00:00 2026-05-30T23:57:53+00:00

Refer below code public void acquire(){ synchronized(a){ print(acquire()); try{ //Thread.sleep(5000); synchronized(this){ wait(5000); } print(I

  • 0

Refer below code

     public void acquire(){
        synchronized(a){
            print("acquire()");
            try{
                //Thread.sleep(5000);
                synchronized(this){
                    wait(5000);
                }
                print("I have awoken");
                print("" + a);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        print("Leaving acquire()");
    }


    public void modify(int n){
        print("Entered in modfy");
        synchronized(a){
            try{
                //Thread.sleep(5000);
                synchronized(this){
                    wait(5000);
                }
                this.a=n;
                print("new value" + a);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

And

        final SynchoTest ttObj = new SynchoTest();
        Thread A = new Thread(new Runnable(){
            public void run() {
                ttObj.acquire();    
            }
        },"A");

        Thread B = new Thread(new Runnable(){
            public void run() {
                ttObj.modify(97);   
            }
        },"B");
        A.start();
        B.start();

As i know about wait(n), it pauses a thread until notify()/notifyAll() get called or the specified time n is over.

But…

  • In above methods if I directly use wait(n) as I used Thread.sleep(n),
    I get runtime exception.
  • If I synchronize both methods instead of surrounding wait(n) with
    synchronized block then I am not getting any exception but both
    threads get blocked forever.
  • But if I do like I attached ie wait(n) surrounding with synchronized
    block, it is working fine.

Please tell me why? Also tell me why is it not behaving different on positioning synchronized block if I use sleep(n) instead of wait(n)?

My question is about various result of wait(n) on various position of synchronized keyword.

@Gray

notify() or notifyAll(), and wait() must be in a synchronized block
for the object you are waiting on

explained me why I was getting run time exception by positioning synchronized block on various position.

Now please explain me, why

public void method(){
    synchronized(a){
        synchronized(this){
            wait(n);
        }
    }
}

is working fine. But

public synchronized void method(){
    synchronized(a){
        wait(n);
    }
}

is blocking my thread forever.

  • 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-30T23:57:55+00:00Added an answer on May 30, 2026 at 11:57 pm

    It is nothing about positioning synchronized keyword. You are facing problem since you locking other object and try to wait for another. Well @Gray has already been explained it, so not repeating it.

    For your another problem, regarding why both threads are getting blocked;

    Thread A: locks this [A: Runnable]

    Thread A: locks a [A: Runnable]

    Thread B: waiting for this [A: Runnable, B:BLOCKED]

    Thread A: release this (meets wait) [A: TIMED WAITING, B:BLOCKED]

    Thread B: lock this [A: TIMED WAITING, B: Runnable]

    Thread B: waiting for a which is already locked by thread A [A: TIMED WAITING, B:BLOCKED]

    Thread A: waiting for this which is locked by thread B [A: BLOCKED, B:BLOCKED]

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

Sidebar

Related Questions

How can I enumerate through an IDictionary? Please refer to the code below. public
I tried this dummy code below to test unnamed namespace. I have the following
Please refer to this post. I have become able to configure my web.config file
Please refer to the Java code below: class Base{ Base(){ System.out.println(Base Constructor); method(); }
Good afternoon all, I have a class which looks like this: public class Grapheme
refer my below javascript code , how to count rows if i click add
I have the code below to advance through a multi-part form for signup. Need
I have partial C# code for a blocking queue that looks like this: private
Please refer the below code. I want the boxes for Blue and all other
According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from

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.