Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

David Moose

Greenhorn
+ Follow
since Jun 11, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David Moose

Henry Wong wrote:

David Moose wrote:I mean by "cache" is that let's say second thread starts going first and executes the getValue a million times times and after that decides to not go to main memory but just get the value from CPU's register.



Lakshman Arun wrote:Declare count as volatile



Not needed. The synchronization will prevent the accessing of the count variable at the same time. And crossing of the synchronization barrier will flush the processor cache (along with any register copies, which BTW, is not the same as the processor caches).

Having said that, keep in mind that the getValue() method returns a copy of the value. This means that when the method releases the lock, the copy can be different from the original variable.

Henry



Thanks!! So the act of locking in getValue() by second thread will synchronize memory/flush registers,L1,L2 etc Where one I find this in JLS https://docs.oracle.com/javase/specs/jls/se7/html/index.html

I mean by "cache" is that let's say second thread starts going first and executes the getValue a million times times and after that decides to not go to main memory but just get the value from CPU's register.


If one thread frequently calls increment() and second thread frequently calls getValue(). Then what prevents the second thread from caching the count variable and thus returning an incorrect value?
3 years ago
I suggest download some free java database, load all the data into that database and use jdbc to select a fortune from that.
17 years ago
Because test2 object is being created.
I think this behaviour is related to the Behavior of polymorphic methods
inside constructors. Consider
class Base {
Base() { method();}
void method() { System.out.println("Base method");}
}
class Derived extends Base{
void method() { System.out.println("Derived method");}
public static void main(String args[]) {
Derived d = new Derived();
}

}
This will print "Derived method".
17 years ago
Thanks you your replies.
I fully understand how this works.
Now will have to work on registering this fact whenver I read JAVA source code
17 years ago
Hi,
I come from a C++ world and now moving to JAVA.
Consider piece of Code.
void foo(String str)
{
str = "Hello" + str;
}

When one calls foo, the string passed remains unchanged.
Similar thing in C++ (passing object my reference) modifies the object.

Can you explain to me in detail what is happening internally here?
I understand that if I call a method on the object it could get modified (in this case since String in immutable that does not apply).

Does Java copy the reference on the stack? how is it different than in C++ world?

thanks a bunch.
17 years ago