nus-cs2030 / 2021-s2

2 stars 2 forks source link

AY19/20 S2 Q5: Asynchronous Programming #333

Open jcng2308 opened 3 years ago

jcng2308 commented 3 years ago

Summary

Hello, is there anyone who can explain how to derive all these expected outputs?

Description

Post the question that you would like your peers or seniors to answer.

Suggested Answer (if any):

Post your answer or the suggested answer, if any.

Screenshots (if any):

Insert Images here if necessary 2021-04-24 (3)

ShyamalSankar commented 3 years ago

Hello, if I am not mistaken, the join at printAsync(1) means that it will definitely be executed before the main thread ends. Hence, any output that does not contain 1 is not possible. I think 1 must also be the first one out.

Next, I think that the allOf would mean that before 4 is outputted, 2 and 3 must definitely be outputted first (in any order). So anything that has 4 without both 2 and 3 inside is incorrect. The main thread may complete with either 2 or 3 completing without both completing so it is possible for combinations like 13, 12, etc (and ofc just 1 itself).

Again this is just my thought process and may not be a 100 percent correct!

(BTW can I ask where did you find this paper? I couldnt find it on the database)

jcng2308 commented 3 years ago

Hello, if I am not mistaken, the join at printAsync(1) means that it will definitely be executed before the main thread ends. Hence, any output that does not contain 1 is not possible. I think 1 must also be the first one out.

Next, I think that the allOf would mean that before 4 is outputted, 2 and 3 must definitely be outputted first (in any order). So anything that has 4 without both 2 and 3 inside is incorrect. The main thread may complete with either 2 or 3 completing without both completing so it is possible for combinations like 13, 12, etc (and ofc just 1 itself).

Again this is just my thought process and may not be a 100 percent correct!

(BTW can I ask where did you find this paper? I couldnt find it on the database)

I just checked it is still in the database, i can send you the paper if you want! Thanks for the response anyway

ShyamalSankar commented 3 years ago

Its ok! I managed to get it. thanks!

jeremycte commented 3 years ago

For future reference to anyone visiting this thread. The answer are a, e & n are yes. The rest of the other outputs is no.

Some argue why wouldn't (o) be true. Reason being is coz printAsync(1).join(). Join statements must be printed before 2,3 or 4 can be completed.

----Edit---- As what Buwoo mentioned, 2 can output without 3 becoz of the method "allOf", refer to this link https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html Therefore, either 2 or 3 can go first and both can be completed or only one actually completing (vise versa).

KaveenE commented 3 years ago

@jeremycte If am not wrong, shouldn't e be no since we should have a '3' if we have '2'?

Buwoo commented 3 years ago

@jeremycte If am not wrong, shouldn't e be no since we should have a '3' if we have '2'?

2 can output without 3 actually completing, and vice versa. We only need both 2 and 3 to output if 4 outputs too, due to the allOf

Herrekt commented 3 years ago

The latter code requires 1 to be printed first before anything. For 4 to be printed, both 2 and 3 will need to be printed before it (in any order). 2 or 3 can be printed without affecting the other (after 1).

KaveenE commented 3 years ago

Got it, thanks!