Jalada home about archive

Shared Memory Communication and Java's Synchronized Methods

2nd February 2010

These are lecture notes from my Computer Science course, not a general reference for "Shared Memory Communication and Java's Synchronized Methods"

For a concurrent program to be correct it needs decent synchronisation (wow, he uses both spellings…he used a ‘z’ in the title which I copy and pasted, and an ‘s’ everywhere else) and communication.

Shared variables communication

We all know the problems with two tasks updating the same variable. This is why shared-nothing (Erlang) works; because it doesn’t allow this. However if you want to be stupid and shoehorn concurrency into normal paradigms you need mutual exclusion to protect critical sections.

Not only that, but a task may also need to wait before it can do something. For example if you have a bounded buffer and two different tasks - a producer and a consumer - the consumer can’t consume anything from an empty buffer; it must wait for the producer to produce something. As an aside, if you only have 1 producer and 1 consumer and you code carefully you don’t need mutual exclusion.

Old methods of fixing

Not so old

Problem(s) with monitors

Readers-Writers Problem

This has a Wikipedia Page. You can fix this problem using monitors by having entry and exit protocols; but not just using a standard monitor because they don’t give you read-locks and write-locks.

Java’s Synchronized Methods

Java’s Block Synchronization

This provides a mechanism where a ‘block’ of a task can be labeled and syncrhonized. You do something like:

<% highlight java %> public int read() { synchronized(this) { return theData; } } <% endhighlight %>

The argument to synchronized is the object whose lock needs to be obtained before the task can continue.

These are useful. In the example of a ‘SharedInteger’ on the slides, what if you want to do some calculations in between reading and writing from it? Even though SharedInteger has synchronized methods, we want to have the lock the whole time otherwise someone might come along and change it in the middle of our calculation. So you can encapsulate it in a synchronized block to get the lock, then you can do what you like.

Apparently if you do this you’re undermining the encapsulation stuff. Or something.

Then…

I lost track a bit from here. Slide 19+. More stuff about the readers-writers problem and static data.

Comments

blog comments powered by Disqus