Learning program for threads in Java