File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multithreading questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multithreading questions" Watch "Multithreading questions" New topic
Author

Multithreading questions

Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
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...












I m stuck in these few questions, if some one can help me it would be great...... Thanks


Thanks,

Pooja Oza
Alfred Swensen
Greenhorn

Joined: May 25, 2009
Posts: 10

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.


Hope that helps!
Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
Hi Alfred,

Thanks for your help.

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.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
wow those are hard.

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
zero.


SCJP 88% | SCWCD 84%
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
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)


------------------------
Bob
SCJP - 86% - June 11, 2009
Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
Thanks Ken and Bob, i really understood those programs now.......
thanks a lot
neha chaukar
Greenhorn

Joined: Oct 16, 2013
Posts: 4
In the dudes example also, two threads are started with different instances of class i.e.
new Thread(new DudesChat()).start();
new Thread(new DudesChat()).start();

so here also synchronised should not play any role as in chess example.

Help me understand this.
Heena Agarwal
Ranch Hand

Joined: Dec 25, 2013
Posts: 261
    
    4
neha chaukar wrote:In the dudes example also, two threads are started with different instances of class i.e.
new Thread(new DudesChat()).start();
new Thread(new DudesChat()).start();

so here also synchronised should not play any role as in chess example.

Help me understand this.


But both these different instances have the same Dudes. Remember that d is a static reference pointing to a Dudes. Hence that Dudes is shared by all the objects of DudesChat.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multithreading questions