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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:52:41+00:00 2026-05-16T02:52:41+00:00

If we have a method: public void doSomething(){ synchronized(this){ //some code processing here }

  • 0

If we have a method:

public void doSomething(){
    synchronized(this){
        //some code processing here
    }
    String temp = "init"; //instead of i++
    synchronized(this){
        //some other code processing here
    }
}

Is this method equivalent to public synchronized void doSomething()?

Is there any reason not to assume that the thread scheduler in some executions would not result in effectively the same flow as synchronizing the whole function? That is:

  • Thread1 enters the first synchronized block.
  • Thread2 blocks.
  • Thread1 continues with i++ and moves to the second synchronized block while Thread2 remains blocked.
  • As a result, Thread2 enters the method after Thread1 has exited both synchronized blocks.

All I need to know is:

  • Can I count on all execution contexts that both threads (Thread1 and Thread2) can be in the method at the same time? For example, Thread2 in the first sync block and Thread1 in the second sync block to achieve concurrency.
  • Will there be some execution flows where only one thread will be in the method (at a time) effectively serializing the whole flow, making it equivalent to public synchronized void doSomething()?
  • 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-16T02:52:41+00:00Added an answer on May 16, 2026 at 2:52 am

    In some executions it would have the same flow as synchronizing the whole functions, sure – but for it to be truly equivalent to making the method synchronized, it would have to have the same flow for all executions.

    As it is, there’s a possibility that another thread will grab the lock (whether for this method or some other code locking on the same monitor) half way through execution. That couldn’t happen if the method itself were synchronized, therefore they’re not equivalent.

    (As an aside, locking on this is generally considered to be bad practice anyway; I can’t remember the last time I wrote a synchronized method. I lock on privately held monitors instead, so that I know my code is the only code which can possibly lock on them.)

    EDIT: To respond to your edit:

    All I need to know is whether I can
    count on all execution contexts that
    both threads (e.g. Thread1 and
    Thread2) can be in the method at the
    same time, e.g thread2 in the first
    sync block and thread1 in the second
    sync block to achieve concurrency

    Absolutely not! It’s guaranteed that you won’t have two threads both in a synchronized block synchronized on the same monitor.

    You have three sections of code: the first synchronized block, the unsynchronized part, and the second synchronized part.

    Any number of threads can be executing in the unsynchronized part at a time. For any one instance (because you’re synchronizing on this) only one thread can be executing either of the synchronized blocks. If you want to achieve concurrency, you’d have to synchronize on different monitors.

    Furthermore, it sounds like you want guarantees of the scheduler letting another thread grab the lock if it was waiting for it. I don’t believe there’s any such guarantee – a thread executing the first block could release the lock but continue in the same timeslice and re-acquire it before any other threads got in. In some JVMs that may not happen, but I don’t believe there’s any guarantee around it.

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

Sidebar

Related Questions

If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have this method: public void testJSNI2(){ String x = test; } I can
Suppose I have the following piece of code public synchronized void method() { if(something
I have a method similar to: public static void DoSomething (string param1, string param2,
I have the following method: public void PutFile(string ID, Stream content) { try {
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have a thread with this run method: public void run(){ MAPTable t1 =
I have a SomeClass implements InterfaceA . I have a method: public void doSomething(List<SomeClass)
Imagine that you have a method with the following signature: public void DoSomething(Guid id)
I have this code: private void doSomething() throws InterruptedException { WorkerThread w= new WorkerThread(this);

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.