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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:12:39+00:00 2026-05-31T14:12:39+00:00

I have a multithreading program, and I am wondering which of the way using

  • 0

I have a multithreading program, and I am wondering which of the way using “synchronized” is correct.

Way 1 :
I have a class object (which will be passed to multiple threads), called MyClass, inside it:

public synchronized void set(String name) {
    this.name = name;
}

Way 2 :
I have same class, but not having “synchronized” in its set method:

public void set(String name) {
    this.name = name;
}

And the caller will do this :

MyClass myclass = new MyClass();
synchronized(myclass) {
    myclass.set("myclass");
}

Can anyone tell me which one is correct way of implementing multithreading object sharing? I am confused by these two and I tried both, they seems to work correctly.

  • 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-31T14:12:40+00:00Added an answer on May 31, 2026 at 2:12 pm

    Synchronize on the method:

    • You do it in one place,
    • Callers do not have to worry about synchronization,
    • You don’t duplicate the code wherever you need to call
    • Most importantly – if callers omit the synchronize, this will not work

    It depends on the context, really – e.g. in some cases it’s better for callers to do the synchronization, as then they can decide whether the synchronization overhead is worth it. For example, all callers that are known to work on one thread do not need synchronization and this will only slow things down.

    For all things that are not time-critical, it will most likely be better to avoid issues you need to debug while on your 5th coffee at 3am, while your colleagues wave a baseball bat and yell words that are not to be mentioned to kids…

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

Sidebar

Related Questions

I have an application which performs 30 independent tasks simultaneously using multithreading, each task
I have a program which interfaces with a radio I am using via a
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a system which breaks a large taks into small tasks using about
I wrote a program that employs multithreading for parallel computing. I have verified that
I have a method in my project which when placed into its own program
I have a winforms app with multiple GUI threads. I want them to be
Have a doubt in multithreading. Following is my main program to access a file,
we have the exercise to program a webserver in object oriented style. So we
I have a program which I need to call more than 100 times with

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.