Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better understanding of Thread construction race conditions

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?

like image 450
Arung Avatar asked Jan 18 '23 05:01

Arung


1 Answers

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.

like image 156
davogotland Avatar answered Jan 20 '23 08:01

davogotland