Open salteefish opened 3 years ago
I think the possible outputs will still be the same, since the system will wait for printAsync(2) and printAsync(3) to finish regardless, while printAsync(1) can complete anytime or not at all before the program ends.
I think the answer would be X since only (iv) would be possible in the case you mentioned. The join() will make it such that printAsync(2) and printAsync(3) must be completed first before printAsync(1) can even be called, so 2 and 3 must be printed before 1 is printed from printAsync(1). So options (i), (ii) and (iii) won’t be possible. If printAsync(1) takes some time to complete then 1 won’t be printed as well before the main program closes.
I believe (iv) would be the only possible option in the case, unless printAsync(1) manages to complete before the program finishes, so possible values could 23, 32, 231, 321
I believe that (iv) is the only answer since having it before entails that printASync(2) and printAsync(3) has to be run due to the join(). There is no guarantee that printASync(1) will be completed without a join and should not be assumed to be completed.
I also think the answer would be (iv) only because any statement after .join() will not be executed therefore 2 and 3 will get printed first. This link is quite helpful https://nus-cs2030s.github.io/2021-s2/36-async.html
Summary
201718 Sem 1 Finals Paper
Description
Hi i was just wondering, if we were to swap the sequence in which printAsync(1) & CompletableFuture.allOf(printAsync(2), printAsync(3)).join( ) is called, such that CompletableFuture.allOf(printAsync(2), printAsync(3)).join( ) is called before printAsync(1), will the answer for this question still be D? Thanks
Suggested Answer (if any):
Post your answer or the suggested answer, if any.
Screenshots (if any):