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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:49:14+00:00 2026-05-31T09:49:14+00:00

I went through the Oracle tutorial on concurrency and threading and came across the

  • 0

I went through the Oracle tutorial on concurrency and threading and came across the following lines:

Warning: When constructing an object that will be shared between threads, be very careful that a reference to the object does not “leak” prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the following line to your constructor: instances.add(this); But then other threads can use instances to access the object before construction of the object is complete.

Could any one can explain what these lines mean exactly?

  • 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-31T09:49:15+00:00Added an answer on May 31, 2026 at 9:49 am

    imagine you have two threads. left thread and right thread. left thread is responsible for creating worker objects, and right thread is responsible for putting them to work.

    when the left thread has finished creating an object, it puts the object in a location where right thread can find it. it’s a variable, let’s call it w (for worker), and for simplicity, let’s say it’s somehow globally accessible.

    the right thread is a loop. it checks if w is not null. if w in fact has a none null value, then the method do is called on it.

    the class worker looks like this:

    public class Worker {
        private int strength;
        private float speed;
        private String name;
        private String specialty;
    
        public Worker(int str, float spd, String n, String spc) {
            strength = str;
            speed = spd;
            name = n;
            specialty = spc;
        }
    
        public void do() {
            System.out.println("Worker " + name + " performs " + strength + " " + specialty + " at " + speed + " times per minute.");
        }
    }
    

    so it goes something like this (i’ve tried illustrating the two threads by setting their respective commands in one column each. i hope it’s understandable. remember, only one thread is active at once, so that’s why there’s always only instructions in one column at a time)

    left thread                                         | right thread
    ----------------------------------------------------|-----------------------------------------------------
                                                        |
    Worker newWorker = new Worker(                      |               ...right thread sleeps...
                        4,                              |                           .
                        5.2f,                           |                           .
                        "mmayilarun",                   |                           .
                        "multithreaded programming"     |                           .
                    );                                  |                           .
    -- control jumps to constructor of Worker --        |                           .
    strength = str                                      |                           .
    speed = spd;                                        |       !!right thread wakes up and takes the focus!!
                                                        |   
                                                        |   if(w == null) {
                                                        |       Thread.sleep(500);
                                                        |   } else {
                                                        |       //this doesn't happen, since w is still null
                                                        |   }
                                                        |   
                                                        |           ...right thread goes back to sleep...
    name = n;                                           |                           .
    specialty = spc;                                    |                           .
    -- control exits constructor of Worker --           |                           .
    w = newWorker;                                      |       !!right thread wakes up and takes the focus!!
                                                        |       
                                                        |   if(w == null) {
                                                        |       //this doesn't happen, since w is NOT null anymore
                                                        |   } else {
                                                        |       w.do();
                                                        |   }
                                                        |
    

    in this scenario, all is well. left thread set w after the constructor of worker had completed. but isn’t it stupid to do it like that? imagine the savings we could do if we put that call w = instanceOfWorker inside the worker constructor. then we wouldn’t have to worry about remembering to actually set w.

    new constructor of worker looks like this:

    public Worker(int str, float spd, String n, String spc) {
        w = this;
        strength = str;
        speed = spd;
        name = n;
        specialty = spc;
    }
    

    now, the flow of the code could end up looking like this:

    left thread                                         | right thread
    ----------------------------------------------------|-----------------------------------------------------
                                                        |
    Worker newWorker = new Worker(                      |               ...right thread sleeps...
                        4,                              |                           .
                        5.2f,                           |                           .
                        "mmayilarun",                   |                           .
                        "multithreaded programming"     |                           .
                    );                                  |                           .
    -- control jumps to constructor of Worker --        |                           .
    w = this;   // danger!!                             |                           .
    strength = str;                                     |                           .
    speed = spd;                                        |       !!right thread wakes up and takes the focus!!
                                                        |   
                                                        |   if(w == null) {
                                                        |       //this doesn't happen, since w is NOT null at this point
                                                        |   } else {
                                                        |       w.do(); //here, w is not yet a fully initialized object,
                                                        |       //and the output is not the expected (if it works at all)
                                                        |   }
                                                        |   
                                                        |           ...right thread goes back to sleep...
    name = n;                                           |                           .
    specialty = spc;                                    |                           .
    -- control exits constructor of Worker --           |
    

    oracle has a more intricate example with a collection of objects called “instances”. that’s the only difference.

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

Sidebar

Related Questions

I went through the following link which says that the external folders will be
I went through many posts and couldn't find a solution. (I came across with
I just went through a spring transaction tutorial which mentions that there are some
I went through a java tutorial that allowed me to create a text file
I went through a document at Adobe Livedocs that describes working with pdf: http://livedocs.adobe.com/flex/3/html/help.html?content=PDF_1.html
I recently started learning Emacs . I went through the tutorial, read some introductory
I'm currently following: http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/ I went through it all pretty easily, but then when
went through the tutorial http://www.sohtanaka.com/web-design/facebook-style-footer-admin-panel-part-1/ and tried the same to make a fixed top
I went through this tutorial Your First iPhone Application and it worked great. My
I went through the tutorial here: http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application , and I can get a graph

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.