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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:30:44+00:00 2026-05-27T07:30:44+00:00

In JCIP we have a piece of code which looks like this: Listing 4.2:

  • 0

In JCIP we have a piece of code which looks like this:

Listing 4.2:

@ThreadSafe
public class PersonSet {
    @GuardedBy("this")
    private final Set<Person> mySet = new HashSet<Person>(); // line 3

    public synchronized void addPerson(Person p) {
        mySet.add(p);
    }

    public synchronized boolean containsPerson(Person p) {
        return mySet.contains(p);
    }
}

I was wondering if we change the third line to this:

   private Set<Person> mySet = new HashSet<Person>(); // line 3, removes final

Is it true to say that the class is no longer thread-safe because the non-final variable mySet could have been null, even after the constructor exits and a reference to the PersonSet instance is published?

For example, is it true to say that a calling code like this may fail, or am I misunderstanding somthing? :

PersonSet p = new PersonSet();
SendToThreadB(p);

What if I have a restriction that does not allow the field to be marked “final” (like I may have to swap it with a new instance), what solutions are there to ensure that the class is still thread-safe without using final ?

  • 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-27T07:30:45+00:00Added an answer on May 27, 2026 at 7:30 am

    If you remove the final, instances become unsafe when used after unsafe publication. That is, if another thread gets access to the object without going through synchronized/volatile to produce an appropriate happens-before relationship then it may see a partially initialised object. In this case it would probably fail in a relatively safe way giving a NullPointerException on dereferencing the mySet field. In theory, another thread could see the reference to the HashSet, but some or all of the fields of that object may not have been set.

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

Sidebar

Related Questions

I'm relatively new to concurrency in Java (still have yet to read JCIP, but
The well acclaimed book JCIP says this about ThreadLocal usage : It is easy
In JCIP book, Listing 5.19 Final Implementation of Memorizer. My questions are: The endless
JCIP defines Guarded objects as: Guarded . A guarded object can be accessed only
Is there a possibility to set priority to tasks which are executed by Executors?
I have a thread running, calling out to an external web service to retrieve
I'm working with existing code that has an object store in the form of
According to section 6.3.2 of JCIP : Runnable is a fairly limiting abstraction; run
According to Goetz in his book JCIP : Because each thread has its own
I see that the above annotations are used extensively in the book JCIP .

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.