Skip to content

Instantly share code, notes, and snippets.

@diegozr1
Created December 10, 2021 08:22
Show Gist options
  • Save diegozr1/395c058a6837ced6623eed45bb2c3c90 to your computer and use it in GitHub Desktop.
Save diegozr1/395c058a6837ced6623eed45bb2c3c90 to your computer and use it in GitHub Desktop.
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