Disclaimer: I am so java illiterate that I am applying my C understanding to the code snippet.
Java.math.BigDecimal toString is not thread safe
21–30 of 90 posts
Re: Java.math.BigDecimal toString is not thread safe
#22Earlier quoted context omitted.
Using the Java terminology, there is no "happens-before" edge. So every action must appear as if it occurred in the specified order in that thread, but other threads are free to see those actions in a different order. On some machines/CPUs/architectures, this is very easy to organise, since the writes can and will be re-ordered. When this happens you can get the pointer to the string being set, before the contents of…
Yes but String.value is final and initialized in the constructor. So it should be safely published.
So it's not really that BigDecimal isn't thread safe, but that there's a bug in the JIT.
Re: Java.math.BigDecimal toString is not thread safe
#23Earlier quoted context omitted.
The issue may be to do with the guarantees that the JVM gives you about orders of operations and write visibility across threads. The author hints at a possible solution when they say "As we see a non-volatile field stringCache", the key being 'volatile' which is a Java keyword with a specific meaning that constrains the order of operations and write visibility across threads. See http://tutorials.jenkov.com/java-con…
> the key being 'volatile' which is a Java keyword with a specific meaning that constrains the order of operations and write visibility across threads. I must point out that "volatile" in Java means something completely different than what it means in C and related languages. In multithreaded C code, "volatile" is almost always incorrect. There are only a few correct and portable uses of volatile (such as dealing wit…
Re: Java.math.BigDecimal toString is not thread safe
#24Earlier quoted context omitted.
Yes but String.value is final and initialized in the constructor. So it should be safely published.
Looking at the article I see he's running on a raspberry pi. I would guess the problem is that the openjdk aarch32 jit is not correctly implementing the java memory model as final fields should have been initialised before an object is visible in another thread. So it's not really that BigDecimal isn't thread safe, but that there's a bug in the JIT.
Re: Java.math.BigDecimal toString is not thread safe
#25Earlier quoted context omitted.
Yes but String.value is final and initialized in the constructor. So it should be safely published.
Looking at the article I see he's running on a raspberry pi. I would guess the problem is that the openjdk aarch32 jit is not correctly implementing the java memory model as final fields should have been initialised before an object is visible in another thread. So it's not really that BigDecimal isn't thread safe, but that there's a bug in the JIT.
Forgetting this is a big source of bugs in double checked locking in .NET.
Re: Java.math.BigDecimal toString is not thread safe
#26I am a complete java noob, but it seems like in the test program the same variable testBigDecimal is being shared by different threads without any lock or mutex or any sort of concurrency control! Won't any function working on testBigDecimal be thread unsafe if it was not specifically written assuming it was working on a shared object? Why is this news? Disclaimer: I am so java illiterate that I am applying my C unde…
...the bug is that it's not really immutable, insofar as it's using an unsafe mutation under the hood to implement its toString method. So it either needs to document the fact that it's not immutable despite the appearance otherwise (crazy), or fix the problem.
Re: Java.math.BigDecimal toString is not thread safe
#27Earlier quoted context omitted.
Yes but String.value is final and initialized in the constructor. So it should be safely published.
Looking at the article I see he's running on a raspberry pi. I would guess the problem is that the openjdk aarch32 jit is not correctly implementing the java memory model as final fields should have been initialised before an object is visible in another thread. So it's not really that BigDecimal isn't thread safe, but that there's a bug in the JIT.
I would not be surprised if this surfaces on other places as well, AFAIK the JDK is full of such benign data races: https://www.youtube.com/watch?v=UykhZ36W04I&index=13&list=PL...
Re: Java.math.BigDecimal toString is not thread safe
#28I am a complete java noob, but it seems like in the test program the same variable testBigDecimal is being shared by different threads without any lock or mutex or any sort of concurrency control! Won't any function working on testBigDecimal be thread unsafe if it was not specifically written assuming it was working on a shared object? Why is this news? Disclaimer: I am so java illiterate that I am applying my C unde…
Even in C, it is fine for multiple threads to access the same data as long as they all read and don't write it.
Re: Java.math.BigDecimal toString is not thread safe
#29Re: Java.math.BigDecimal toString is not thread safe
#30I am a complete java noob, but it seems like in the test program the same variable testBigDecimal is being shared by different threads without any lock or mutex or any sort of concurrency control! Won't any function working on testBigDecimal be thread unsafe if it was not specifically written assuming it was working on a shared object? Why is this news? Disclaimer: I am so java illiterate that I am applying my C unde…
It is news because either there is bug in the BigDecimal or JVM in question that causes the above assumption to not hold.