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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:18:22+00:00 2026-05-31T04:18:22+00:00

Consider a snippet from a class : // tail call to show void showNextPoint(){

  • 0

Consider a snippet from a class :

    // tail call to show
void showNextPoint(){
    Point p = new Point();
    p.x = 10;
    p.y = 12;
    show(p);
}

// multiple calls to show
void showPoints(){
    Point p = new Point();
    p.x = 10;   p.y = 12;
    show(p);
    p.x = 20;   p.y = 22;
    show(p);
    p.x = 30;   p.y = 32;
    show(p);
}

void show(Point p){
    // use p in some way
    // can p's state be changed safely ?
            // can a new thread be started to work with p safely ?      
}

In a multithreaded use-case , can the object p be considered as published safely from the showPoints() or showNextPoint() ? What conditions must hold true in show() for this ?

If show(Point p) is guaranteed 1) not to start a new thread and 2) not to change p’s state , can the showPoints() method be considered as safely publishing p ?

  • 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-31T04:18:23+00:00Added an answer on May 31, 2026 at 4:18 am

    The show() method gets a reference to a Point p. If the caller afterwards makes a change to p, it is making a change to the same Object. If show() changes p in any way then it is updating the same p object that the caller is using. If a thread is forked that has this p then you have a race condition as to which update to p is performed in which order.

    Point p = new Point(1, 2);
    show(p);
    // p.x == 100 here
    
    void show(Point p) {
       p.x = 100;
    }
    

    You asked:

    void show(Point p){
        // use p in some way
        // can p's state be changed safely ?
        // can a new thread be started to work with p safely ?      
    }
    

    It cannot be changed safely without affecting the caller’s object, no. A new thread also cannot make changes to p without it affecting the caller’s object when memory synchronization happens.

    In a multithreaded use-case , can the object p be considered as published safely from the showPoints() or showNextPoint() ? What conditions must hold true in show() for this ?

    It all depends on how you’ve written the code — what the contracts are. If a method makes changes to an object then it should be documented. If the caller does not wish this then either you should pass in a copy of a Point object that the method could “own” or the method itself should copy Point itself.

    The complications around synchronizing changes to object is one of the reasons why many Java objects are immutable. You may consider doing this to Point. Make it so the x and y values cannot be changed which means that the show method and any threads it spawn can safely use the argument without worrying about changed being made to it from the caller.

    Edit:

    If show(Point p) is guaranteed 1) not to start a new thread and 2) not to change p’s state , can the showPoints() method be considered as safely publishing p ?

    @JohnVint had some better points about this but I thought I’d add my thoughts. I’m not sure what you are asking here and I’m not sure what you think “safely publishing” implies. The devil is in the details. If show() doesn’t start a new thread then yes, the code you have posted will work and it’s okay to use the same mutable Point object.

    Any method that takes an object argument but does not change the object in any way can say that it is “safely using” (I guess) the object. Since showPoints() constructed the object and has the only reference to it then that is certainly safe as well. It’s when p gets modified and there is another thread involved that everything changes.

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

Sidebar

Related Questions

Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code snippet within a class private static Object _syncroot = new
Consider a snippet of CSS code from a .css file in Visual Studio 2010
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Please consider the following snippet from an implementation of the Interpreter pattern: public override
Consider this code snippet: transaction.executeSql( 'SELECT * FROM pv_master;', [], function(transaction, result) { if(result.rows.length
Consider this snippet: class SomeClass(object): def __init__(self, someattribute=somevalue): self.someattribute = someattribute def __eq__(self, other):
I am having problem understanding Priority Inversion Snippet from the article: Consider there is
Consider this snippet: use strict; use warnings; my $data = 1; my $packed =
Consider the following WSH snippet: var query = GetObject(winmgmts:).ExecQuery(SELECT Name FROM Win32_Printer, WQL, 0);

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.