Also the programs are not direct translations of each other - a direct translation to Java would use varhandle and opaque (equivalent to relaxed), and then would not contain a data race.
You can go the other way, porting that c++ to Java using varhandle and opaque memory order.