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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:16:32+00:00 2026-06-16T04:16:32+00:00

Straight out from Java concurrency in Practice : @ThreadSafe public class SafePoint { @GuardedBy(this)

  • 0

Straight out from Java concurrency in Practice :

@ThreadSafe
public class SafePoint {
  @GuardedBy("this") private int x, y;
  private SafePoint(int[] a) { this(a[0], a[1]); }
  public SafePoint(SafePoint p) { this(p.get()); }
  public SafePoint(int x, int y) {
     this.x = x;
     this.y = y;
   }
  public synchronized int[] get() {
     return new int[] { x, y };
  }
  public synchronized void set(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

The above is a Thread-safe class : since its setters are synchronized.
I understand also why the getter doesn’t individually return x / y but instead returns an array.
I have 2 questions .
Why ?

  1. private SafePoint(int[] a)
  2. public SafePoint(SafePoint p) { this(p.get()); } instead of this(p.x,p.y);
  • 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-16T04:16:33+00:00Added an answer on June 16, 2026 at 4:16 am

    Because calling this(p.x, p.y) is not atomic. In other words, imagine the following thread interleaving:

    • Thread1: read p.x
    • Thread2: p.set(otherX, otherY);
    • Thread1: read p.y and call this(p.x, p.y);

    The x and the y are now from two different points and possibly not consistent.

    On the other hand, p.get() reads x and y atomically (it is synchronized) and therefore guarantees that the x and the y in the array are from the same point.

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

Sidebar

Related Questions

This is straight from Java Concurrency in Practice. The following causes a Deadlock :
This is straight from the Java Docs : This class and its iterator implement
If you want to get straight to the problem, skip this paragraph. As practice,
This piece of code was taken straight out of the example from: https://github.com/caolan/async#seriestasks-callback var
I have taken this little snippet straight out of some code I'm working on:
I am fairly new to JAVA and especially concurrency, so probably/hopefully this is fairly
I find out that the GeneralPath class in Java only provides methods to check
I'm having trouble running from ant. When I run straight from the class file
I have taken an example straight out of the MSDN. I want to create
I am interested in figuring out the best way to do straight through processing

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.