...
Since you cannot lock on primitive types, you cannot take a direct lock on the items in the balances
array. Instead, you have to create an array of object Object (locks).
The code above avoids deadlock because every thread requests monitors in the same order, since it is always acquiring the locks in numeric order.
...