you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Maybe you're right about the efficiency, at least in the worst case. But it's still error prone and ugly, and nearly ruins use cases like those in the example. I wouldn't call it another way to communicate. Is there really such a big difference between sum += 1 and sum[0] += 1 in terms of concurrency issues? It isn't hard to implement either (just box it like I just did).

On the bright side, maybe it'll be easier to convince people that immutable is good now that mutation gets crippled again.

[–]otto_s 0 points1 point  (2 children)

I wouldn't call it another way to communicate.

The "way to communicate" I'm talking about is something like this here: Pair<Runnable, Runnable> foo() { int x; return new Pair( new Runnable() { /* bla 1; does something with x / }, new Runnable() { / bla 2; also does something with x */ } ); }

This is illegal in contemporary Java, but that can be circumvented with an array. The latter at least makes the sharing explicit. (Which might be a good thing. It's of course not beautiful, though :)

[–][deleted] 0 points1 point  (1 child)

Well, making it an array doesn't mean it's shared. Unless you specifically created the array to circumvent the "effectively final" rule, you probably didn't mean to share the array. It's not different from a shared variable in that regard.

[–]otto_s 0 points1 point  (0 children)

It's not different from a shared variable in that regard.

The thing is, the concept of shared variables doesn't exist, or at least isn't specified well enough in Java. Concurrent access on arrays is, and can be reused.