Correct way to handle java's multithreading