• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Threads: synchronized method (K&B 1.5, page 733)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I have some problems with SELF TEST, exercise 1 (K&B, Java 1.5, Chapter 9, page 733)

According to the book the answer is Wallace-1 Gromit-1 Gromit-2 Wallace-2

I compiled the code and got another result. Wallace-1 Wallace-2 Gromit-1 Gromit-2




Thank you all in advance
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a self test question (basically did you create the program yourself or was it given in the book)?? In my K&B for SCJP6 book, there's no question like this. I would say that the output is unpredictable. It can be the output given in the book, or what you got or a few other possible outputs...
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B (Java 5) doesn't say the output is "Wallace-1 Gromit-1 Gromit-2 Wallace-2". Instead they ask "Which of the follwing is a possible result ?" and give you 7 options. Of these "Wallace-1 Gromit-1 Gromit-2 Wallace-2" is the only one possible.

Of course there are other possible outputs, as Ankit already mentioned. But these are not part of the options given in the question.

Hope this helps.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can't predict the answer, can be any combination. But the values should be there!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, the synchronized metod synchronize the thread execution. I am also tempted to say the result is unpredictable, but for the synchronized keyword.
I did try running the code and I got the following results

First try
-----------
Wallace-1
Wallace-2
Gromit-1
Gromit-2

Second Try
------------
Gromit-1
Gromit-2
Wallace-1
Wallace-2

Third Try
----------
Gromit-1
Gromit-2
Wallace-1
Wallace-2

The point is it really depends which thread is running first. So the answer is unpredicatable.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There are two instances of Thread and two Messengers: (The thread objects overide their run() with that of Messenger)
Synchronization applies to individual instance, inside each instance, execution comes by the order of 1, 2. It is guaranteed. But JVM doesn't guarantee which code segment of specific thread come first and second and so forth. Say, it may execute like:

Thread A, Thread B

-> A, 1st statment
-> put A in a runnable status
-> B, 1st statement
-> B, 2nd statement
-> put B in a runnalbe status
-> A, 2nd statement
-> B, 3rd statement
.....

Therefore, many results of combination. But inside each thread, statements run through by the order of source code.
In this case, whatever combination, Wallace-2 won't come before Wallace-1, and Gromit-1 must come before Gromit-2.
 
Mea Cook
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really speaking I don't understand te role of synchronized in this context. Can anybordy help?



this is the same as



What object will be locked: from class Thread or Messager? And how long?
I thought, that if 1 Thread is locked, then runs to the end the 2 Thread and then the 1 Thread runs to the end.
But the answer shows, it is not correct...

Thank you
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lock is for Objects. In your case it's for Messager object.

The period is up to the end of your run() method or block.

We can't predict which thread will get the first chance to be executed, but if one thread got a chance, the it'll finish the job, then only the other thread will have a chance to be executed.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mea,

Welcome to the ranch!

You tackled an interesting question here, and the only 'Javaranch-ish' recommendation I have is that you should show ranchers not only the code from the mock question you're studying, but also the entire question - in this case (and in many cases), the parts of the question outside of the code are also essential to a valid discussion.

Thanks,

Bert
 
Mea Cook
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bert! I am only few days on ranch, but I like it very much.

I'll do my best and next time write the full questions not only code.

This question is resolved for me and I close it
reply
    Bookmark Topic Watch Topic
  • New Topic