• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Waiter and Notifier Example

 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is in this waiter and notifier example in lines 25 and 50 we are calling synchronized on the variable message but it is a private instance variable "message" in both Waiter and Notifier classes. So how does changing instance variable "message" in Notifier Class change the value of message in the Waiter class? I know it is a silly question but there is some gap in my understanding



Output:
Inside run method of Waiter
Waiter thread going to wait
Inside run method of Notifier
Returned:sum=10
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:My question is in this waiter and notifier example in lines 25 and 50 we are calling synchronized on the variable message but it is a private instance variable "message" in both Waiter and Notifier classes. So how does changing instance variable "message" in Notifier Class change the value of message in the Waiter class? I know it is a silly question but there is some gap in my understanding


You have to remember the difference between a reference variable and the object that it references. Yes. You are correct that both the Waiter and Notifier instances have their own reference variables. And yes, those references variables are declared as private. So, you are correct in that the two instances can't access each other's reference variables.

However, if you follow the code, you will also notice that both reference variables are pointed to the same message instance. And the code isn't changing the reference variables, they are changing the instance that it is pointing to.

Henry
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Now I have created another example posted below. The reference variables of the two classes does not point to the same object when the other variable is changed as in the below example.

I have created 2 classes, Employee and Emp and both have reference variables. I am changing the name in the Emp class. Their values are different when I see the output.

How is it that it is in conflict with the example I used in my first post of this thread.



Output:
Raja
Hello Raja
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
How is it that it is in conflict with the example I used in my first post of this thread.


Not sure what you mean by "in conflict"... but ... yeah, your second example shows that having completely different instances mean that changes don't affect each other.

Henry
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By conflict I mean that in the second example which I posted, change in 1 reference variable of one class does not affect the reference variable of another class. But the first example shows that change in reference variable of one class changes the other as well since both reference variables point to the modified object.

I need some clarity here
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:By conflict I mean that in the second example which I posted, change in 1 reference variable of one class does not affect the reference variable of another class. But the first example shows that change in reference variable of one class changes the other as well since both reference variables point to the modified object.

I need some clarity here


Nothing to clarify. Your explanation is correct.

Henry
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you misunderstood me.

My question is why there is inconsistency in the behavior of the two examples?

 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:No, you misunderstood me.

My question is why there is inconsistency in the behavior of the two examples?


In the first example, you have two different references and they are both pointing to the same object. In the second example, you have two different references and they are each pointing to two different objects.

... which example are you confused with? What is inconsistent with what you are seeing?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hmmm.... in thinking about this some more, perhaps you are asking why the first variable change doesn't create another object, while the second variable change does?

To understand that, you have to understand how Java does string concatenation. Since strings are immutable, it can't actually change the string object. A concatenation is actually syntactic sugar -- it creates a StringBuilder instance, and uses it to create a new string result (by appending the string operands).  And since, in your second example, you are changing the reference, the two references are now pointing to different objects.

Henry 
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comparison between the two examples are as follows:

The Waiter class of Example 1 is replaced with Employee class in Example 2

The Notifier class of Example 1 is replaced with Emp class in Example 2

The message object of Example 1 is replaced with a String object in Example 2.

When I modify the message object it reflects in all classes but when I modify the string object it does not reflect in other classes?

Why is it happening like that? That is my question.


 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
When I modify the message object it reflects in all classes but when I modify the string object it does not reflect in other classes?


Not sure if you got my answer, as I seemed to have sneaked it in with the previous post...

In the first example, you indeed did modify the message object. In the second example, you did *not* modify the string object. Also, that is impossible, as string objects are immutable. Instead, as explained above, you simply modified (actually, set differently) one of the two string reference variables (specifically, the name variable of the Emp instance).

Henry
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in my second example line 30, I have used this.name="Hello "+name; and I have not used new operator in this line of code.
 
Campbell Ritchie
Sheriff
Posts: 51343
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:. . . this.name="Hello "+name; and I have not used new operator in this line of code.
No. You are creating a String with a literal; as far as the JVM is concerned, the String Literal "Hello " does not tell it to create a String object. That String literal IS A String object to the JVM. Have a look at the Java® Language Specification:
A string literal is a reference to an instance of class String
Then you are using the concatenation operator (+) on that String with another operand, which happens to be a String too. That JLS section will tell you that:-
The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. . . .

The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
Since you are not using a constant expression (sometimes called a compile time constant), that expression with + creates a new String and there is no need to use the new operator. Nor are you changing the values of any Strings if the result is “newly created.”
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects are created when I use the following line of code and what are they? The value assigned to name is "Raja".

 
Campbell Ritchie
Sheriff
Posts: 51343
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many do you think? Using the javap tool with such code may help.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:How many objects are created when I use the following line of code and what are they? The value assigned to name is "Raja".

as you mentioned name is assigned value "Raja".
compiler infer it as-
if the string pool contains the 2 string literals,then instance for the literals will not be instantiated or otherwise instance will be created and get stored in pool.
so the object created may be 1,2 or 3.[1 is for the StringBuilder].

Note:String pool is a kind of cache on heap that stores the string literals as a string object and can be reused(without creating a copy) till and unless the jvm close.
 
praveen kumaar
Ranch Hand
Posts: 242
4
Chrome Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[EDIT:for second code snippet in my previous post]

Sorry for the mistake.

Kind Regards,
Praveen.
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try. Lets assume "Hello" is not in the string pool. So that is one object. Concatenation with name should create new object since string is immutable. So I am assuming 2 objects will be created. Correct me if I am wrong



 
Campbell Ritchie
Sheriff
Posts: 51343
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is usually another object in the background that does the concatenating. The current implementation uses a StringBuilder instance. Let us try a slightly different bit of code. Assume the Scanner object has already been created and doesn't count.Let's try an example with javap:-
javac StringConcatenationDemo.java
javap -c StringConcatenationDemo
We have four objects visible: look at lines 4: 7: 11: and 26: Since we are including "Hello, " this time, that is one more than you had in your code. The putfield instruction in 29: means the field and the object created in 26: are both the same object.
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


We have four objects visible: look at lines 4: 7: 11: and 26

Internally it may be using StringBuilder objects but if I consider only String objects created in the above example, then there are 3. One is "Hello,"(assuming it is not in the String pool). Second is "Raja"(again assuming it is not in the String pool) and third is concatenated object?



 
Campbell Ritchie
Sheriff
Posts: 51343
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote: . . . if I consider only String objects created in the above example, then there are 3. . . .
Yes, 3, but that is different from what you said at first.
 
raja singh kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code, if the string pool is empty then totally are there 2 String objects created?




In the above code if the string pool is empty, then there are 3 String objects created?
 
Campbell Ritchie
Sheriff
Posts: 51343
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String pool isn't empty because those String literals go into the pool at class-loading time, but otherwise the numbers of String objects look correct to me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic