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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:55:33+00:00 2026-05-27T05:55:33+00:00

I have a doubt on the java synchronization mechanism. Let us take the following

  • 0

I have a doubt on the java synchronization mechanism. Let us take the following piece of code–

  class A {
     int instance=0;
     synchronized void met1() {
       instance=instance +1;
        ....
        instance = instance+2*3;

      }

In the above method met1, we need to make it synchronized in a multi threaded enviroment because it is modifying an instance variable of an object. However in this piece of code —

class A {

     synchronized void met1() {
       local=local +1;
        ....
        local = local+2*3;

      }

The method met1 is modifying a local variable, which I think will be unique for each thread that executes that method. So in this case, when a thread is modifying only a local variable, is it necessary to make the method synchronized ?

  • 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-27T05:55:33+00:00Added an answer on May 27, 2026 at 5:55 am

    In your second example, local is not exactly local, because you are not declaring it inside met1. Probably you meant:

     synchronized void met1() {
       int local = 0;
       // ...
       local=local +1;
        ....
        local = local+2*3;
    
      }
    

    Then yes, you are correct, since each method call happens on the stack of the calling thread, method-local variables exist only the corresponding thread’s stack and don’t need synchronized access.

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

Sidebar

Related Questions

I have one doubt regarding casting. public void Test(out T a, out T b)
i have some doubt about PHP based web crawlers,can it run like the java
In a certain Java class for a Struts2 web application, I have this line
hi i m a beginner in java i have a doubt: in the case
I have a doubt from the book Efective Java. The doubt is regarding equals
class Dims { public static void main(String[] args) { int[][] a = {{1,2,}, {3,4}};
i have a simple doubt in android programming. I am not familiar with java
I am new to java, I have a doubt: I am retunging con from
I have a basic doubt regarding object creation in java.Suppose i have two classes
I have a question on boolean return types. Check the following code: Code Sample

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.