aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes DudesChat clarification please? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "DudesChat clarification please?" Watch "DudesChat clarification please?" New topic
Author

DudesChat clarification please?

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
On October 17 of this year another ranch hand posted the code from the K&B book, chapter 9 (Threads):


Bartender Ankit Garg responded to the question by stating:
Hint: The chat method in Dudes class is synchronized and the method is called on the same instance by both the threads...


May I have some clarification concerning this?. I certainly see two threads working here, as a result of the code on the lines I identified as "new1" and "new2". Am I correct in stating that there is only one instance of Dudes running as a result of the line of code I identified as "set Dudes"? Is this the one "same instance" that Ankit is referring to?

And also: Is it true that the code on lines "new1" and "new2" DO NOT create new instances of DudesChat, but rather create new threads using the DudesChat class as a runnable target?

Thank you for your time.
Gary

G
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18724
    
  40

http://www.coderanch.com/t/514073/java-programmer-SCJP/certification/Thread-exercise


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18724
    
  40

Gary Marshall wrote:
And also: Is it true that the code on lines "new1" and "new2" DO NOT create new instances of DudesChat, but rather create new threads using the DudesChat class as a runnable target?


Take a look at those two lines again. You can clearly see that DudesChat instances are being created -- in addition to the Thread instances that are being created.

Henry
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121

Thank you, Henry. The answer to the next question in the chapter (the "ChicksYack" code) cleared this up where it states that:

.... When run () is invoked, it is with a new instance of ChicksYack.....


OK. So, again, when Ankit Garg is saying that:
...The chat method in Dudes class is synchronized and the method is called on the same instance by both the threads...

..is Ankit referring to the static Dudes class as the "same instance" that both threads are calling the chat method on? Is he saying that since the Dudes class is a static class there is only one Dudes class, and that one Dudes class is the "same instance" that both threads would be calling on?


Thank you for your patience. I'm just trying to understand the "same instance" thing.

Thank you
Gary

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18724
    
  40

Gary Marshall wrote:
..is Ankit referring to the static Dudes class as the "same instance" that both threads are calling the chat method on?


yes
 
Don't get me started about those stupid light bulbs.
 
subject: DudesChat clarification please?