Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Concurrency Issue on My mac? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Concurrency Issue on My mac?" Watch "Concurrency Issue on My mac?" New topic
Author

Concurrency Issue on My mac?

john sayeau
Ranch Hand

Joined: Aug 23, 2010
Posts: 33
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Please UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 31, 2004
Posts: 11343

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
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

Joined: Sep 29, 2008
Posts: 12617

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
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

Joined: Sep 29, 2008
Posts: 12617

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
Thanks everyone.
 
Consider Paul's rocket mass heater.
 
subject: Concurrency Issue on My mac?
 
Similar Threads
Threads and synchronisation
SYnchronization
Question on Synchronized mehods
Using synchronized still doesn't solve the "lost update" problem
Need Explanation of unpredicted output even if methods are synchronized