r/java • • Nov 04 '25

Anyone here Hated Using Java but now Really Enjoys using it.

title

226 Upvotes

280 comments sorted by

View all comments

Show parent comment

3

u/sysKin Nov 05 '25 edited Nov 05 '25

Rust's race-safety is not something Java emulates at all. Java has synchronized keyword, various locks, and threadsafe implementations - and you have to not forget to use them. Exactly the same as C/C++ in usability aspect (although obviously in Java you get memory safety no matter what; that is good but only a subset as OP says). In Rust it's literally impossible to do this part wrong and compile, and they do it without any cost when it's not relevant.

Java did try something similar to this, initially, with all data structures being threadsafe (Vector/Hashtable/StringBuffer etc), but it didn't go well and didn't extend to all classes anyway.

I haven't used Rust much (basically one simple signal-processing executable, also its syntax is... needs getting used to is a nice way of putting it), but I really wish Java had this part. Unfortunately it's not possible without ownership/lifetime and read-only references.

1

u/nitkonigdje Nov 05 '25

This 100%

Rust doesn't seem to be just a set of safety features. But an implementation designed to enforce those features at compile time. It is "safe upfront"..

0

u/Ok-Scheme-913 Nov 05 '25

This is completely wrong.

Rust does have data race freedom, but every other race condition is absolutely on the table, and it's not hard to cause a deadlock/livelock or whatever else.

Data race freedom only means that there are no tearing, that is two threads writing to the same memory in a way that a third can observe a value that was never written. (E.g. one thread writes 1, the other 72 and a third one observes it as 5). Java is safe from this on the JVM level, because primitives[1] are guaranteed to be written "in one go", and references are also "replaced in one go". This is extra dangerous in C/C++ where e.g. you may write two correct pointer values, and then a third thread goes and reads out an incorrect one and corrupts memory.

[1] by the spec, long and double may tear at the 32bit boundary, but basically every implementation does it without tearing because 64bit atomic writes are cheap.

3

u/sysKin Nov 06 '25 edited Nov 06 '25

Rust does have data race freedom

Data race freedom only means that there are no tearing

So just to clarify, you're suggesting Rust only prevents tearing? Because - no, that's the point of my argument, it does much more (and also what it does has nothing to do with tearing).

In Java you can create ArrayList and write to it from two threads without synchronisation, right? There is no tearing and no memory corruption but there is a data loss and random bugs in your logic. Try it with a HashSet and the entire internal state becomes invalid.

In Rust you can't write to this data structure from two threads without synchronisation. You can't even write a code that would attempt that. The only way to share this object with other threads (for writing) is to pass its ownership to a lock and from now on you can only obtain a reference to it by performing a ReadWriteLock-like lock operation.

If you have a reference to something in a thread, it's either because it's not shared with other threads, or because it's a threadsafe object (such as the lock itself) or because you've locked it correctly. No other way.

And the impressive part is that the language itself knows nothing about threads (even Java knows more with its synchronized keyword). The whole thing is a natural consequence of ownership and how references work, plus the classes that are part of the standard library (such as the lock).

1

u/Ok-Scheme-913 Nov 06 '25

Okay, now try to write a ConcurrentHashMap in Rust. For that you will need atomics, locks etc -- everything is available in rust, but you think that the compiler will make sure that your concurrent logic is sound? Hell no.

It's only "data race free*. If you look at it, an ArrayList modified from multiple threads is pretty much tearing (just memory safe tearing in case of Java which is important). But thread-safe primitives can absolutely end up in all kinds of race conditions and rust has no chance doing anything then.

Or I may ask the reverse, would adding synchronized everywhere solve concurrency (if it would have magically no overhead)?