Pooja Oza wrote:Can some one help me understand the output of the following programs ..... The examples are from SCJP 6 by K&B book, in the self test of multithreading chapter...
The Dudes question:
You run the thread with two newly created DudesChat instances. But that's not the point as the run() method only refers to the d variable which is exactly the same for both threads.
As it is the same, the synchronized keyword in fragment I works as expected, the output will always be "yo yo dude dude" which appears in none of the possible answers.
With fragment two you have a typical unsynchronized case i.e. both methods could theoretically be entered and run at the same time. So F is at least possible.
The exam authors claim that "yo" must always be the first output although I think that it is theoretically possible that one thread sets the flag variable and immediately after that the other thread starts and completes the chat method so that "dude dude" is printed first. Any ideas if this might be possible?
The Chicks question:
You create two threads, each with a *new* instance of the ChicksYack In this new instance the c variable (which is not static as the d in the above example!) was never initialised so if the thread scheduler calls the run() method of these new instances you get a NullPointerException when it tries to call the method .yack(...) on the uninitialized variable c.
The Chess question:
Here, too, you start two threads with different instances of a class. So whether or not you put synchronized before a method is irrelevant as they both only lock their instance.
The output is thus random for both examples. As there are only two threads there can never the case where three different id values are printed so option D is invalid.
But i still do not get that how come the output of the Chess and the Chicks problems have the output options like : 4 4 2 2 or whatever..... where and how do these numbers come from? Can you please clear that if possible.
In the Chicks question, note that the go() method which spawns the threads happens to initialize c just before it
starts the threads based on two NEW instances of the ChicksYack class. This is there only to confuse people. You
would have to manually initialize the c field in the instances of the ChicksYack class used by the threads for the
program to work properly (or just make c static, as they say in the answer).
I think the thread id's are just examples that they made up. The only thing you would want to be sure about is
that there can only be 2 id's, so answers with 4's, 2's AND a 3 are going to be wrong. Actually it's pretty confusing
with respect to the first question because I'm not certain that one of the spawned threads couldn't have an id of
Looking at the last one first..... Two threads are instantiated and each one is passed a SEPARATE DISTINCT Chess object. So, when each thread starts up it refers to a different instance of Chess object. What THAT part means is that the synchronization applied to the method in I is useless. The method in each object synchronizes on its OWN "this" and they are different so there will be no coordination at all.
The other part on the number sequence comes from this: Each thread that is started has an ID and that ID will stay the same for it's entire life cycle. So any answer with three values is wrong because there are only two runnable objects being run, so there will only ever be two IDs show up. The one that has 4, 2, AND three, is obviously wrong. Since the run method defines what each "runnable" will do, the ID will only be printed twice for each instance of the runnable. So any answer that shows a given ID 3 times is wrong.
The only issue that remains is the order. Since they are unsynchronized with respect to each other the IDs can really come out in any pattern/permutation.
(ie 4422, 2244, 2442, 4224, 2424, 4242)
SCJP - 86% - June 11, 2009