• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Multithreading questions

 
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
Netbeans IDE VI Editor PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Pooja Oza
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ken and Bob, i really understood those programs now.......
thanks a lot
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic