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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:09:09+00:00 2026-05-14T02:09:09+00:00

Goetz’s Java Concurrency in Practice , page 41, mentions how this reference can escape

  • 0

Goetz’s Java Concurrency in Practice, page 41, mentions how this reference can escape during construction. A “don’t do this” example:

public class ThisEscape {
    public ThisEscape(EventSource source) {
        source.registerListener(
            new EventListener() {
                public void onEvent(Event e) {
                    doSomething(e);
                }
            });
    }
}

Here this is “escaping” via the fact that doSomething(e) refers to the enclosing ThisEscape instance. The situation can be fixed by using static factory methods (first construct the plain object, then register the listener) instead of public constructors (doing all the work). The book goes on:

Publishing an object from within its constructor can publish an incompletely constructed object. This is true even if the publication is the last statement in the constructor. If the this reference escapes during construction, the object is considered not properly constructed.

I don’t quite get this. If the publication is the last statement in the constructor, hasn’t all the constructing work been done before that? How come is this not valid by then? Apparently there’s some voodoo going on after that, but what?

  • 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-14T02:09:09+00:00Added an answer on May 14, 2026 at 2:09 am

    The end of a constructor is a special place in terms of concurrency, with respect to final fields. From section 17.5 of the Java Language Specification:

    An object is considered to be
    completely initialized when its
    constructor finishes. A thread that
    can only see a reference to an object
    after that object has been completely
    initialized is guaranteed to see the
    correctly initialized values for that
    object’s final fields.

    The usage model for final fields is a
    simple one. Set the final fields for
    an object in that object’s
    constructor. Do not write a reference
    to the object being constructed in a
    place where another thread can see it
    before the object’s constructor is
    finished. If this is followed, then
    when the object is seen by another
    thread, that thread will always see
    the correctly constructed version of
    that object’s final fields. It will
    also see versions of any object or
    array referenced by those final fields
    that are at least as up-to-date as the
    final fields are.

    In other words, your listener could end up seeing final fields with their default values if it examines the object in another thread. This wouldn’t happen if listener registration happened after the constructor has completed.

    In terms of what’s going on, I suspect there’s an implicit memory barrier at the very end of a constructor, making sure that all threads “see” the new data; without that memory barrier having been applied, there could be problems.

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

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't need to treat select elements differently, the val… May 16, 2026 at 5:51 pm
  • Editorial Team
    Editorial Team added an answer Here is some tips and check: 1/ Separate this line… May 16, 2026 at 5:51 pm
  • Editorial Team
    Editorial Team added an answer The Debian and Ubuntu projects use a script checkbashisms, that… May 16, 2026 at 5:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

According to Brian Goetz's Java Concurrency in Practice JVM can't exit until all the
I was reading some of the concurrency patterns in Brian Goetze's Java Concurrency in
Referring to Brian Goetz's article Are all stateful Web applications broken? for IBM developerWorks,
I've read plenty of articles about tuning GC in Java and have often wondered
I've heard about this happening in non thread-safe code due to improperly constructed objects
From the various online articles on Java 7 I have come to know that
In the spirit of the latest podcast where Joel mentioned he'd like some simple
I have a relatively complicated generic type (say Map<Long,Map<Integer,String>> ) which I use internally
Imagine you have a class with dozens of private member variables. Each member variable
The script I am trying to run via shell_exec in PHP requires an environmental

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.