• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Concurrency Issue on My mac?

 
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time.
 
Author
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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
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
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.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic