Java introduced a great deal of improvement in version 5 with the new concurrency facilities it provided, and had built on it in subsequent versions of the language.
These facilities are great and powerful, but they still can sometimes be hard to use correctly. I will cover concurrency in the traditional (and concurrency utilities) way in another post. This post is just about introducing a couple of alternatives to these.
In this post I’ll introduce the Software Transactional Memory (STM) model for concurrency. In a later post I’ll introduce the Actors model in Java.
STM: Very much like the transactions we are use to in DBs but working with objects in memory, keeping the ACI properties of transactions.
In this model we separate identity from state. This basically means that we have some entities that hold an inmutable state that can change over time. We’ll understand this better with the example that will follow.
The main advantage of this separation is that there is no need to block or synchronize as the state doesn’t change.
I’ll write a piece of code now and explain how it works. For the example I will use Akka and its support for STM with Multiverse.
Let’s assume we want to create a very simplistic kind of Stack (No validations, no anything) that only supports push, peek and pop and it should be thread safe. The simplistic code would be something like the following (No thread safe).
- public class StmStack {
- private static int SIMPLISTIC_BIG_SIZE = 1024;
- private int top=0;
- top --;
- return value;
- }
- return elements[top];
- }
- elements[top+1] = value;
- top++;
- }
- }
It is very simple to see that there exist a thread safety problem with the state variable ‘top’. The problem is simply that this variable is shared mutable state. A variable that can be accessed, read and changed concurrently by mutliple threads. And of course the elements array itself has the same problem.
The common solution to this problem, would be to synchronize the access to the three methods. This approach is not hard to follow here in this very simple example, but even here it has a couple of drawbacks. One of them is that synchronizing and lock acquiring affects performance. Other can be that for example the peek method is not changing state, and in an application that will use peek more than any other method (more reading and moderate writing) no more than one thread will be able to peek. Also we need to remember to synchronize every method that we put in this class that will either read or write. When things start to get a little complicated (more locks, more granularity, more mutable shared state) understanding synchronization and locking logic is not very easy, and debugging synchronization and concurrency problems is hard to say the least.
We’ll see now the code using STM. This will look a bit complex
- public class StmStack {
- final TransactionFactory factory =
- new TransactionFactoryBuilder().setBlockingAllowed(true).setTimeout(new DurationInt(6).seconds()).build();
- return new Atomic(factory) {
- if (top.get() < 0) {
- StmUtils.retry();
- }
- top.swap(top.get() - 1);
- return value;
- }
- }.execute();
- }
- final TransactionFactory factory =
- new TransactionFactoryBuilder().setBlockingAllowed(true).setTimeout(new DurationInt(6).seconds()).build();
- return new Atomic(factory) {
- if (top.get() < 0) {
- StmUtils.retry();
- }
- return elements.get().get(top.get());
- }
- }.execute();
- }
- new Atomic() {
- elements.swap(new Elements(elements.get(), value, top.get()));
- top.swap(top.get() + 1);
- return "";
- }
- }.execute();
- }
- public int elements() {
- return top.get()+1;
- }
- }
- class Elements {
- private static int SIMPLISTIC_BIG_SIZE = 1024;
- this.elements = elements.elements;
- this.elements[top + 1] = newElement;
- }
- Elements() {
- }
- return elements[index];
- }
As we talked before the use of Ref helps to separate the ID of the reference to its state so in this case we have a Ref that will point to a different Elements object in different moments of time, but will not modify the one it has currently directly, it will just replace it. We do the same with the ‘top’ variable, making it a Ref instead of an Integer.
The next thing is the implementation of the different methods. As we can see we are wrapping each method content in an Atomic object, and putting the content inside the atomically method. This will ensure that the operation runs inside in a transaction.
The transaction ensures that all the values in the Ref variables are consistently changed or rolled back in case some other transactions modify them in the meantime, retrying automatically if that is the case. We can also observe the ‘retry’ method call. This method help us retry the transaction in the case a pre-condition is not met to run the transaction in the first place.
I’ll show a couple of running examples now(Just the relevant code):
first we run 7 push operations in different threads:
final StmStack stack = new StmStack();
push(stack);
push(stack);
push(stack);
push(stack);
push(stack);
push(stack);
push(stack);
sleep();
System.out.println(stack.elements());
The output:
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
pushing asdad
Number of elements: 7
Event though we called the push on the stack only 7 times, we can see that the push method (actually just the transaction part) was executed more than 20 times. This is because of all the retrying when multiple threads collide in the transaction execution. We can see that maybe for heavily writing applications STM transactions might not be the best solution because of all the retrying.
Now we run just 1 push, and then 6 peeks.
push(stack);
sleep(2000);
peek(stack);
peek(stack);
peek(stack);
peek(stack);
peek(stack);
peek(stack);
sleep(8000);
System.out.println(stack.elements());
And the output
pushing asdad
pushing asdad
peeking
peeking
peeking
peeking
peeking
peeking
Number of elements: 1
We can see that no retry is needed for the peeking. We can see that STM is better suited for multiple reads, low-to-moderate writes.
So with STM we have a model that avoids the use of explicit locking, offers great concurrency performance, helps encourage the use of immutable types and just mutable References, makes explicit the parts that we expect to be executed atomically.
It also dynamically and automatically handles threads and contention, and can help mitigate or entirely remove deadlocks as we don’t deal with locking or lock orders anymore.
STM is an alternative worth considering if we have requirements for concurrency that adjust to this model.
A lot more information in:
AKKA Documentation
And
1 comment:
Que carajo mas empalomado
Post a Comment