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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:48:45+00:00 2026-06-12T03:48:45+00:00

I’m testing out a code where I have 2 methods and a statement in

  • 0

I’m testing out a code where I have 2 methods and a statement in each method is synchronized.

private final Object obj1 = new Object();
private final Object obj2 = new Object();

public void method1(int result)
{
 //there's a loop to create delay

 synchronized (obj1){
   sum = sum + result;
 }

 //there's a loop to create delay

}


public void method2(int result)
{
  //there's a loop to create delay

 synchronized (obj2){
   sum = sum - result;
 }

 //there's a loop to create delay

}

What I’m basically doing is I add a result to sum then subtract the same amount then print the sum. So my initial and final amount should be constant.

The problem is I don’t know why the final amount is not the same as initial amount when I use 2 diff objects as lock. HOWEVER it’s constant when I use ‘this’ object as lock for both statements. But using ‘this’ as object didn’t make execution time faster than using method synchronized.

I’m still new to synchronization concept so any help is appreciated.

  • 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-12T03:48:47+00:00Added an answer on June 12, 2026 at 3:48 am

    When you specify an object in the synchronize block, that object is used like a lock for that block: only one synchronize block can use a lock object at any time.

    When you use two different objects as locks, they become two different locks: two threads can lock them both at the same time – one thread per lock. This explains the inconsistent results: two threads can grab two separate locks concurrently, and modify the sum concurrently, resulting in incorrect calculations.

    You should use a single lock to protect a single resource from concurrent access. This is what happens when you use this; the same would happen if you used obj1 or obj2 in both synchronize blocks, except in this case a user of your object would not be able to cause your methods to block forever by synchronizing on your object, and intentionally not releasing the lock.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.