Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DudesChat clarification please?

 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic