단일 스트림
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class MyLogger {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
public static void log(Object obj) {
String time = LocalTime.now().format(FORMATTER);
System.out.printf("%s [%9s] %s\n", time, Thread.currentThread().getName(), obj);
}
}import util.MyLogger;
public class HeavyJob {
public static int heavyTask(int n) {
MyLogger.log("calculate " + n + " -> " + (n * 10));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return n * 10;
}
public static int heavyTask(int n, String name) {
MyLogger.log("[" + name + "] " + n + " -> " + (n * 10));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return n * 10;
}
}V1 - 단일 스트림
V2 - 스레드 직접 사용
V3 - 스레드 풀 사용
Fork/Join 패턴

Last updated