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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:23:47+00:00 2026-06-06T21:23:47+00:00

I have simple code: public class testing { private static Object objToSync = new

  • 0

I have simple code:

public class testing {
    private static Object objToSync = new Object();

    public static void main(String[] args) {
        String obj1 = null;
        synchronized(objToSync){
            System.out.println("something one");                    
            doSomething();
            System.out.println("something three ");         
        }

        doSomething();      
    }

    private static void doSomething() {
        synchronized(objToSync){
            System.out.println("something two");
        }       
    }

I have read several things but still getting confused with this one. Why does the doSomething in the main gets called? Is it not suppose to wait till the synchronized object gets unlocked? Sorry if I am sounding stupid, i am just confused.

  • 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-06-06T21:23:49+00:00Added an answer on June 6, 2026 at 9:23 pm

    Is it not suppose to wait till the synchronized object gets unlocked?

    The lock is held by the thread, so the fact that you’re synchronizing on it twice (in the case of the first call to doSomething in main) doesn’t matter, it’s on the same thread. If another thread then tried to enter a synchronized block on objToSync, that other thread would wait until this thread released all of its locks.

    Your code will do this:

    1. Enter main
    2. Get a lock for the current thread on the objToSync object
    3. Output “something one”
    4. Call doSomething
    5. Get a second lock for the current thread on objToSync
    6. Output “something two”
    7. Release the second lock for the current thread on objToSync
    8. Return from doSomething
    9. Output “something three”
    10. Release the first lock for the current thread on objToSync
    11. Call doSomething
    12. Acquire a new lock (for that same thread) on objToSync
    13. Output “something two”
    14. Release that lock
    15. Return from doSomething
    16. Return from main

    Here’s an example using two threads:

    public class SyncExample {
    
        private static Object objToSync = new Object();
    
        public static final void main(String[] args) {
            Thread second;
    
            System.out.println("Main thread acquiring lock");
            synchronized (objToSync) {
                System.out.println("Main thread has lock, spawning second thread");
                second = new Thread(new MyRunnable());
                second.start();
                System.out.println("Main thread has started second thread, sleeping a moment");
                try {
                    Thread.currentThread().sleep(250);
                }
                catch (Exception e) {
                }
                System.out.println("Main thread releasing lock");
            }
            System.out.println("Main thread sleeping again");
            try {
                Thread.currentThread().sleep(250);
            }
            catch (Exception e) {
            }
            System.out.println("Main thread waiting for second thread to complete");
            try {
                second.join();
            }
            catch (Exception e) {
            }
            System.out.println("Main thread exiting");
        }
    
        static class MyRunnable implements Runnable {
    
            public void run() {
                System.out.println("Second thread running, acquiring lock");
                synchronized (objToSync) {
                    System.out.println("Second thread has lock, sleeping a moment");
                    try {
                        Thread.currentThread().sleep(250);
                    }
                    catch (Exception e) {
                    }
                    System.out.println("Second thread releasing lock");
                }
                System.out.println("Second thread is done");
            }
        }
    }
    

    Output:

    Main thread acquiring lock
    Main thread has lock, spawning second thread
    Main thread has started second thread, sleeping a moment
    Second thread running, acquiring lock
    Main thread releasing lock
    Main thread sleeping again
    Second thread has lock, sleeping a moment
    Main thread waiting for second thread to complete
    Second thread releasing lock
    Second thread is done
    Main thread exiting
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple code: package mygame; public class RunGame { public static
I have the following simple code abstract class A { public abstract void Test(Int32
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have this simple code public String toString() { **Iterator it = list.iterator();** String
I have this simple class: public class LuceneUtil{ private final EntityManager entityManager; public LuceneUtil()
I have a simple test class: public partial class TEST_CLASS { public string IP
I have this sample code for async operations (copied from the interwebs) public class
I have created the simple web service. Code: [ServiceContract] public interface ITsdxService { [OperationContract]
I have an object in a simple test scenario that uses EF Code First

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.