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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:58:45+00:00 2026-05-22T18:58:45+00:00

Give the following code class Test{ double x; public void synchronized a() { x

  • 0

Give the following code

    class Test{
       double x;
       public void synchronized a()
       { 
          x = 0;
          //do some more stuff
       }
       public void b() 
       { 
          x = -1; 
       } 
    }

Can the thread in a(), in the middle of modifying x be preempted by a thread that calls b() on the same object?

Isn’t synchronized method be executed like one single atomic operation?

I believe the other way is possible(thread in b() can be preempted by the thread that calls a() on the same object since b() is not guarded my the Test object lock).

Can some one shed some light on this?

  • 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-22T18:58:45+00:00Added an answer on May 22, 2026 at 6:58 pm

    synchronized only stops other threads from acquiring the same monitor. It in no way makes the operation atomic. In particular:

    • Side-effects of the method can be observed by other threads which aren’t trying to synchronize on the same monitor
    • If an exception occurs, there’s no sort of roll-back
    • Other threads can access and modify the same data used by the synchronized method, if they aren’t synchronized on the same monitor

    b() isn’t synchronized, so it’s entirely possible for one thread to be executing a() and another to be executing b() at the same time.

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

Sidebar

Related Questions

Why does the following code NOT give an error, nor any type of a
I'm using the following code within a VB 6.0 application to allow give the
For homework, I was given the following 8 code fragments to analyze and give
What recommendations can you give for a system which must do the following: Load
Give me some of your thoughts on which is a better coding practice/makes more
Kindly give me some example that how we can use INSERT INTO ALL STATEMENT
In the following code, why does 2 give output but not 3? The removechars
I'm looking for a way to programmatically recreate the following effect: Give an input
The following two queries each give the same result: SELECT column FROM table GROUP
The following SQL: SELECT notes + 'SomeText' FROM NotesTable a Give the error: The

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.