This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrency Issue on My mac?

 
john sayeau
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. :)
 
john sayeau
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?)
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic