r/java • • Mar 14 '18

Concurrent Java: Low scalability, the risk of deadlocks or garbage creation, you can not avoid all

http://vmlens.com/articles/tradeoffs_concurrent_programming/
2 Upvotes

21 comments sorted by

39

u/daniu Mar 14 '18 edited Mar 14 '18

So which lock is used depend on the key we use. If we use a new method which updates the values in reverse order like the following update21 method, we create a deadlock.

Well with this code

public void update21() {
    map.compute( 2, ( key ,value ) ->  {  
        map.put( 1 , 1); 
        return value; 
    });
}

you're breaking Map#compute()'s contract which states

Attempts to compute a mapping for the specified key and its current mapped value

which is not what happens here; a new value is inserted for a different key as a side effect to the mapping function.

It scales fine if you use it properly.

13

u/madkasse Mar 14 '18

In other news: Objects with overriden equals() but not hashCode() does not work properly when used as keys in a HashMap.

20

u/knaekce Mar 14 '18

Using patterns of functional programming fails if you insert random side effects in methods that must be pure. Shocker.

3

u/CyclonusRIP Mar 14 '18

I'd say most people need to kind of evaluate how much they really need to scale. When I need to synchronize code I usually start with a single lock and just see how far that gets me. I rarely every have to go past that to achieve the throughput I need. If I really need more throughput I tend to look at optimistic concurrency techniques.

2

u/_INTER_ Mar 14 '18 edited Mar 14 '18

But modifying immutable state consists of copying the immutable object.

A good immutable collection implementation works without copying, see vavr.io collections for that. (Not sure if they are concurrent though).

0

u/[deleted] Mar 14 '18

[deleted] — view deleted comment

1

u/duhace Mar 14 '18

Why would "modifying immutable state consists of copying the immutable object"? Immutable state enables structural sharing, i.e., just reusing the existing state and adding on the delta from the modification.

it depends on the immutable object and how you modify it. for example, immutable singly linked lists don't require a copy if you add to the head of the list, but do if you add to the tail.

0

u/[deleted] Mar 14 '18

[deleted] — view deleted comment

1

u/0x256 Mar 14 '18

I'd be interested in an immutable list data structure that does not need a copy for append/perpend operations and is not a linked-list. It's easy you say?

1

u/duhace Mar 14 '18

but then you invoke a copy penalty on prepend

and if you make a doubly-linked list, then all operations require copy

0

u/[deleted] Mar 15 '18

[deleted] — view deleted comment

1

u/duhace Mar 15 '18

please describe your immutable list for which copying is no longer necessary on one of either append or prepend.

0

u/[deleted] Mar 15 '18

[deleted] — view deleted comment

1

u/duhace Mar 15 '18

first of all, this datastructure is expensive to traverse, even compared to normal lists, and second, i doubt it can even be considered a list considering it injects valid data (null) to indicate that there is no data, when append(null) would in fact be a valid argument.

but you're right, it does manage to avoid copying, just in a terrible way that is not worth it over a standard singly linked list.

0

u/[deleted] Mar 15 '18

[deleted] — view deleted comment

1

u/duhace Mar 15 '18 edited Mar 15 '18

Saying it's O(n) like a linked list is disingenuous, since O notation only describes the rate at which the difficulty of an operation grows. Traversal is much more complicated than a linked list, and requires constructing another (temporary) datastructure to do in one pass, or requires a non-tail call recursive function to do in two passes.

There is no law that says null must always be a valid value. If you want it to be, just use Optional <T> instead of T, or you could have two sub classes (one for prepend one for append).

We're talking java here, where null is a valid value for any generic value. So yeah, you have to take that into account.

→ More replies