I have a method that writes a file and it can be a bit slow. So I did this to avoid blocking the rest of actions:
// things A
method();
// things B
public void method(){
new Thread(() -> {
// slow stuff
}).start();
}
However it still takes a while until things B are executed after things A and I was wondering if I should do this instead:
// things A
new Thread(() -> {
method();
}).start();
// things B
public void method(){
// slow stuff
}
Or, in other words, does calling a method with a thread inside it imply waiting for the method to end, and therefore the thread?
Just to clarify things: I want to know if both options would be the same or if in the first option the thread should finish for the B instructions to start.
CodePudding user response:
Does calling a method with a thread on it imply waiting for the method to end, and therefore the thread?
No. To highlight that the thread isn't a factor, I took your original code:
method();
// things B
public void method(){
new Thread(() -> {
// slow stuff
}).start();
}
And sprinkled a bunch of System.out.println()
calls throughout:
- one before calling
method()
, and one right after - one at the start of
method()
, and one at the end - one at the start of the thread body, and one at the end
Also:
- the thread does "slow stuff" by calling
Thread.sleep()
for 2 seconds - each
println()
includes current date time - the output is crudely formatted with indentation to show the start/end pairs of each output
import java.time.LocalDateTime;
public class ThreadExample {
public static void main(String[] args) {
System.out.println("main >> " LocalDateTime.now());
new ThreadExample().method();
System.out.println("main << " LocalDateTime.now());
}
public void method() {
System.out.println(" method >> " LocalDateTime.now());
new Thread(() -> {
System.out.println(" thread >> " LocalDateTime.now());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(" thread << " LocalDateTime.now());
}).start();
System.out.println(" method << " LocalDateTime.now());
}
}
Output below, which shows a few things:
- the first second output lines in
main
(before and after callingmethod()
) are basically instantaneous – 09:10:22.036305 start, 09:10:22.037957 end, for a difference of 0.001652 seconds - same for
method()
, nearly instantaneous with start of 09:10:22.037564 and end of 09:10:22.037897, which is a difference of 0.000333 seconds - the start and finish outputs in
thread
are where the waiting is, starting at 09:10:22.037920, ending at 09:10:24.041840; total difference of 2.00392 seconds which is the 2 seconds we asked for withThread.sleep()
plus the tiny amount of execution time similar to the others (0.00392) - perhaps most importantly for this question, it's clear that both the original caller (in
main()
) andmethod()
both finish beforethread
finishes ~2 seconds later
main >> 2022-06-17T09:10:22.036305
method >> 2022-06-17T09:10:22.037564
method << 2022-06-17T09:10:22.037897
thread >> 2022-06-17T09:10:22.037920
main << 2022-06-17T09:10:22.037957
thread << 2022-06-17T09:10:24.041840