Hi.
I'm working my way through head first java. In chapter 15 they talk about using "synchronized" on a method and have an example in their book (copied in below).
The example works perfectly on my linux machine but on my snow leopard machine running 1.6.0_20 java the synchronized keyword seems to be ignored.
Anyone? Is there a setup issue I need to address for the mac. I've tried compiling and running from both the jdk and eclipse.
Here's the code:
here is the output when run on the mac:
The balance is: 1
The balance is: 3
The balance is: 2
The balance is: 5
The balance is: 7
The balance is: 8
The balance is: 9
The balance is: 4
The balance is: 6
The balance is: 10
The balance is: 11
The balance is: 12
The balance is: 13
The balance is: 14
The balance is: 15
Ernest Friedman-Hill
author and iconoclast
Marshal
Only increment() is synchronized, so only increment() is serialized -- i.e., it's only calls to increment() that are prevented from happening at the same time. A symptom of failure would be the sum being something other than 15 at the end of the program -- i.e., a value greater than 15 showing up, or all the values being smaller than 15. But what's happening is that the variable is being incremented 15 times, and that's working fine. The three threads are sampling and printing the value of the variable at unsynchronized times, and that might be confusing -- but it's perfectly correct.
Also, when looking at buggered (actually meant buffered, but my typo is funnier) output, things can seem somewhat disjointed depending on a bunch of different things outside of our control.
David Newton wrote:Also, when looking at buggered (actually meant buffered, but my typo is funnier)...
Glad to know I'm not the only one who does that. :)
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
john sayeau
Ranch Hand
Joined: Aug 23, 2010
Posts: 33
posted
0
Ernest Friedman-Hill wrote:Only increment() is synchronized, so only increment() is serialized -- i.e., it's only calls to increment() that are prevented from happening at the same time. A symptom of failure would be the sum being something other than 15 at the end of the program -- i.e., a value greater than 15 showing up, or all the values being smaller than 15. But what's happening is that the variable is being incremented 15 times, and that's working fine. The three threads are sampling and printing the value of the variable at unsynchronized times, and that might be confusing -- but it's perfectly correct.
I copied this example right from the book. So I guess it's not a perfect demonstration of the topic. I thought about what you said and moved
System.out.println("The balance is: " + balance);
From run() to increment() and now it works.
Thanks!
(unless there's something more?)
john sayeau wrote:System.out.println("The balance is: " + balance);
From run() to increment() and now it works.
It worked before, though.
john sayeau
Ranch Hand
Joined: Aug 23, 2010
Posts: 33
posted
0
David Newton wrote:
john sayeau wrote:System.out.println("The balance is: " + balance);
From run() to increment() and now it works.
It worked before, though.
I think I understand. If it wasn't working I'd have something other than all the numbers between 1 and 15 generated only once. I guess that's what you guys were trying to tell me.
All that wasn't happening, the way I'd expected, was the order the values were printed and that's what I ended up fixing.
Do I have it?
Yep, indeed you do. That's one thing about streams, buffering, and output in general--you don't necessarily know how that's being handled behind-the-scenes (and it's even worse in IDEs), so it can throw you off sometimes. Concurrency is tricky enough without being mislead by such things! :)
john sayeau
Ranch Hand
Joined: Aug 23, 2010
Posts: 33
posted
0
Thanks everyone.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.