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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:25:35+00:00 2026-06-14T09:25:35+00:00

Suppose I have an immutable Model class: class Model { final String id; Model(String

  • 0

Suppose I have an immutable Model class:

class Model {
    final String id;
    Model(String id) {
        this.id = id;
    }
}

And I have a custom Task class:

class Task extends BlaBlaTask {

    final Model model;

    Task(Model model) {
        this.model = model;
    }

    // runs on a background thread
    void doInBackground() {
        // do smth with model, e.g.:
        String id = model.id;
    }
}

And both Model and Task instances are created on the main UI thread. However doInBackground() is run on another thread. Is this code wrong? Should I add synchronization, e.g. something like this:

class Task extends BlaBlaTask {

    Model model;

    Task(Model model) {
        setModel(model);
    }

    // runs on a background thread
    void doInBackground() {
        // do smth with model, e.g.:
        String id = getModel().id;
    }

    private synchronized void setModel(Model m) {
        model = m;
    }

    private synchronized Model getModel() {
         return model;
    }
}

P.S. I am working on Java 1.4, and the code probably can be run on a multi-core CPU.

  • 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-14T09:25:38+00:00Added an answer on June 14, 2026 at 9:25 am

    I’m not familiar anymore with the Java memory model of Java 1.4, but I don’t see why you would need synchronization.

    If you’re starting a thread, then the new thread will see everything you have written before starting the thread.

    And if you’re passing the task to an existing thread, the publishing mechanism should have all the necessary synchronization in place to make sure that the thread sees everything that has been written before the publication. That shouldn’t be the task’s job to synchronize anything, it should be the Queue’s job (or any other way you use to pass the task from one thread to the other)

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

Sidebar

Related Questions

Suppose I have a custom object set up in a class similar to this.
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have import scala.collection.immutable.TreeMap val tree = new TreeMap[String, List[String]] Now after above
Suppose I have a class with a collection of immutable types, that I would
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Let's suppose that I have the following class which tries to be immutable public
Suppose I have a stupid little case class like so: case class Foo(name: String,
Suppose i have : models.py: class Person(models.Model): user = models.OneToOneField(User) def get_album(self): return self.album_set.all()
Does python have immutable lists? Suppose I wish to have the functionality of an
Suppose I have a static method of my class that returns an object of

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.