Java code to schedule threads