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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:27:54+00:00 2026-05-29T13:27:54+00:00

I am studying Java concurrency right now. I have a question about synchronized and

  • 0

I am studying Java concurrency right now. I have a question about synchronized and locks.

For any mutable data, we should put all the method accessing the data in the same lock.

But, what does same lock mean?

Example:

public class SynchronizedInteger{
    private int value;
    public synchronized int get(){return value;}
    public synchronized void set(int value){this.value=value;}
}

So, my question is why this two methods are in the same lock? I know they are, but I would like to know why? And, does it mean that all the synchronized methods in the class are in the same lock?

Edit:

so, if I added one more method to the class:


public synchronized void printOneToHunder(){

for(int i=0;i<100;i++) System.out.println(i);

}

This method will also be included into same block as setter and getter? So, other thread cannot run this method when there is one thread using either setter or getter?

And, what if I change the class to below:

public class SynchronizedInteger{
    private int value1;
    private int value2;
    public synchronized int get1(){return value1;}
    public synchronized void set1(int value){this.value1=value1;}
    public synchronized int get2(){return value2;}
    public synchronized void set2(int value){this.value2=value2;}       
}

For my understanding right now, only one thread can call these method at one time. So what’s the way to make one thread can modify value1 and the other thread to modify value2???

Thanks a lot for your kindly clarifications!!!!

  • 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-29T13:27:55+00:00Added an answer on May 29, 2026 at 1:27 pm

    All non-static methods that you declare synchronized behave essentially as if the code was:

    public synchronized int get() {
      synchronized(this) {
        // the method code
      }
    }
    

    i.e. there is an implicit lock on this taken. So all non-static synchronized methods will lock the instance on which they are called.

    Static synchronized methods take a lock on the class object itself, rather than an instance of that class.

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

Sidebar

Related Questions

I'm studying for Java Programmer Certification (SCJP) exam. A question about exceptions, when handle
I have been studying Java for a few months and am now starting to
Hey peoples, I've been studying Java for a couple of weeks, and have decided
Total noobie question here. I'm just learning Java, and studying passing arguments to functions.
While studying about JMX, I have seen one of the important feature of it
I'm studying pascal-fc for a concurrency test. I'm familiar with Java's threads and its
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
I am studying Data Structures in java and I am having difficulty with using
I'm studying Java Concurrency in Practice and there it is explained why the following
I've been studying Java EE 6 and I am confused about the difference between

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.