nus-cs2030 / 2021-s2

2 stars 2 forks source link

19/20 Sem2 Q5 #405

Open darylyeejh opened 3 years ago

darylyeejh commented 3 years ago

I think 1 is guaranteed to be printed out because of join(), but does join() also guarantee that 1 will be printed out before the other numbers are printed out? If yes/ no then why

Picture1

ryo-wijaya commented 3 years ago

Yeah I think so. If I'm not wrong join() is blocking. So the answers for this question has to start with 1.

JoelToh404 commented 3 years ago

yes all the answers have to start with a 1

bandytan commented 3 years ago

.join() waits for 1 to be completed and prints 1 out first. Since there is no .join() on 2,3,4, they might not be completed and printed out. If they are printed, 4 has to be after 2 or 3. The order of 2 and 3 does not matter.

brendancjz commented 3 years ago

So what should the answer for this qn be? (a), (e), (i), (j), (n)?

Buwoo commented 3 years ago

Fyi this problem has been discussed on #333

rljw commented 3 years ago

So what should the answer for this qn be? (a), (e), (i), (j), (n)?

I think the answer for this should be (a), (e) and (n). Options (i) and (j) are not possible because.thenRun(() -> printAsync(4))comes after the .allOf() function, meaning that 2 and 3 both have to be printed before 4 would be printed. Hope that helps!

danieltwh commented 3 years ago

So what should the answer for this qn be? (a), (e), (i), (j), (n)?

I think the answer for this should be (a), (e) and (n). Options (i) and (j) are not possible because.thenRun(() -> printAsync(4))comes after the .allOf() function, meaning that 2 and 3 both have to be printed before 4 would be printed. Hope that helps!

Yeap agreed. In this case, 1 is definitely printed. However, 2 or 3 or both could be printed after 1. If 4 is printed, then both 2 and 3 (in any order) has to be printed. Hence, the output should be (a), (e) and (n)

chelleadel commented 3 years ago

Hi! I agree with the others that the method .join() waits for the internal process to complete its execution before allowing any other programs to continue on. Therefore, it is a guarantee that 1 will come out first and the answer is A E I J N.

clementyee303 commented 3 years ago

Hi! I agree with the others that the method .join() waits for the internal process to complete its execution before allowing any other programs to continue on. Therefore, it is a guarantee that 1 will come out first and the answer is A E I J N.

I and J are not possible. For 4 to complete, both 2 and 3 has to complete first.

ErMingYong commented 3 years ago

since 1 has a .join(), 1 will definetly be exeuted and completed first -> 1 will surely bet he first output however, 2 and 3 are executed without a join and it runs for undetermined amount of time, hence they could either both be completed, or either being completed or none at all. Lastly, 4 has a .thenRun(), hence can only be executed when both 2 and 3 are completed. Therefore, the output has to have 1 being the first, 2 and 3 can be in random order or even none and lastly, should there be a 4, then both 2 and 3 have to be present and placed before 4