Fix a race condition in Java Thread callbacks