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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:24:00+00:00 2026-06-03T09:24:00+00:00

I am having a code like below in my class that will act like

  • 0

I am having a code like below in my class that will act like my ‘Task’ class. i will create multiple instances of this class and pass it to the threadpool

class Task {

    public void doJob() {

        String s = informationDto.getName();

    }
}

DTO:

class InformationDto{

    private String name;

    public getName(){}

    public setName(String n){}

}

i am wondering about the informationDto.getName(); statement. will it be return me the correct name always in a multithreaded environment?

what happens if the getName() method is static?

i want to use informationDto each for every Task class i am creating. what will be the best way to do this?

1) i want to keep one InformationDto for each Task class.
2) since instance variables are not thread safe i am wondering if i access getName() method (since name is a instance variable ) would i get the correct name that i set in the Task class?
3) it is like if i use a util class inside the Task class will it be thread safe? if not how can i use kind of util instance in a thread safe manner?

  • 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-03T09:24:01+00:00Added an answer on June 3, 2026 at 9:24 am

    I am still not exactly clear about your problem but enough said in comments, will try to give you an answer.

    I would assume the getName() does this:

    public getName() {
        return name;
    }
    

    and setName() does this:

    public setName(String name) {
        this.name = name;
    }
    

    where name is:

    private String name;
    

    Having an instance of this class used by multiple threads does not guarantee that everyone will see the same thing when they ask getName(), not until it’s guaranteed that no one calls setName() on it while the others are working.

    One way to guarantee the getName() to return a consistent and the same value is to lock it down. You do so by making your object immutable:

    private final name;
    
    public InformationDTO(String name) {
      this.name = name;
    }
    

    This way name is guaranteed not to change and you can pass your object safely to as many threads as you wants knowing that no one can change it. This will however make it not a perfect POJO in a way that you can’t set that property. If you need to be able to use setName() (directly or indirectly through some kind of a framework) this won’t do it for you.

    What else can go wrong with this instance in a multithreaded environment? not much. The getName() won’t return a partial state, like half the old value and half the new value being written by another thread. You’re safe there. Making the getName() static doesn’t change much. It will now be a class method, not an instance method with the name also becoming static which is very much a global state tat you don’t want in your backyard.

    Hope this answers your question or at least gives you enough clues to figure out your next steps.

    UPDATE. Like it was pointed out in comments, if you do expect the name to change and want to be sure all threads read the most recent value, you can use volatile when declaring the member variable:

    private volatile String name;
    

    Using volatile will ensure your threads “communicate” on modification events and don’t trust their local caches.

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

Sidebar

Related Questions

Would having a nice little feature that makes it quicker to write code like
I am having an issue converting type. I was trying code like this (minimal,
iam having controller like below which is not registered in routes table public class
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed
How to convert the text file through vb6 code. Having filename like clock.fin, time.mis,
How to copy the text file through vb6 code. Having Source filename like clock.fin,
If you look at the method below you will se a declared local class
The code below is going to be called from a website so having a
The entirety of my programs are below. The problem I am having is that
I have a very specific problem. I have a user control (code below) That

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.