Created
December 10, 2021 08:22
-
-
Save diegozr1/395c058a6837ced6623eed45bb2c3c90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.SecureRandom; | |
import java.time.LocalDateTime; | |
import java.util.Optional; | |
import java.util.concurrent.TimeUnit; | |
public class BusyTask { | |
private final int time; | |
private final String id; | |
private LocalDateTime start = null; | |
private LocalDateTime end = null; | |
private BusyTask(int start, int end) { | |
this.time = new SecureRandom().nextInt(end) + start; | |
this.id = String.format("BW%05d", time); | |
} | |
public static BusyTask newBlueInstance() { | |
return new BusyTask(3, 12); | |
} | |
public static BusyTask newRedInstance() { | |
return new BusyTask(12, 24); | |
} | |
public int execute() { | |
try { | |
start = LocalDateTime.now(); | |
//some I/O operation | |
TimeUnit.SECONDS.sleep(time / 2); | |
TimeUnit.SECONDS.sleep(time / 2); | |
end = LocalDateTime.now(); | |
} catch (InterruptedException ignored) { | |
} | |
return time; | |
} | |
public Optional<LocalDateTime> start() { | |
return Optional.ofNullable(start); | |
} | |
public Optional<LocalDateTime> end() { | |
return Optional.ofNullable(end); | |
} | |
public int getTime() { | |
return time; | |
} | |
public String getId() { | |
return id; | |
} | |
} | |
public class UseBusyTask{ | |
public static void main(String) | |
{ | |
final BusyTask task = BusyTask.newBlueInstance(); | |
task.id(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment