#C13895. Task Scheduling Simulation: Synchronous vs. Asynchronous Execution

    ID: 43483 Type: Default 1000ms 256MiB

Task Scheduling Simulation: Synchronous vs. Asynchronous Execution

Task Scheduling Simulation: Synchronous vs. Asynchronous Execution

In this problem, you are required to simulate task execution under two different modes: synchronous and asynchronous. When executed synchronously, five tasks are performed one after the other; hence, each task will start only after the previous one has finished, resulting in longer overall execution time. In asynchronous mode, the tasks are started concurrently, leading to a much shorter total execution time.

For each task, a message is printed when the task starts and when it completes. In synchronous execution, the messages follow the order:

(\text{Task Sync-1 starting}) (\text{Task Sync-1 completed}) (\text{Task Sync-2 starting}) (\text{Task Sync-2 completed}) ... and so on up to Sync-5.

In asynchronous execution, all start messages are printed first, followed by all completion messages in order:

(\text{Task Async-1 starting}) (\text{Task Async-2 starting}) (\text{Task Async-3 starting}) (\text{Task Async-4 starting}) (\text{Task Async-5 starting}) (\text{Task Async-1 completed}) (\text{Task Async-2 completed}) (\text{Task Async-3 completed}) (\text{Task Async-4 completed}) (\text{Task Async-5 completed})

Your solution should read from standard input a single word (either sync or async) and produce the corresponding output logs on standard output. Use appropriate asynchronous programming constructs in the respective languages to simulate concurrency.

inputFormat

The input consists of a single line from stdin containing either sync to execute tasks sequentially or async to execute tasks concurrently.

outputFormat

The output is a series of log messages printed to stdout. For synchronous mode, the messages should appear in the following order:

Task Sync-1 starting Task Sync-1 completed Task Sync-2 starting Task Sync-2 completed Task Sync-3 starting Task Sync-3 completed Task Sync-4 starting Task Sync-4 completed Task Sync-5 starting Task Sync-5 completed

For asynchronous mode, the output should be:

Task Async-1 starting Task Async-2 starting Task Async-3 starting Task Async-4 starting Task Async-5 starting Task Async-1 completed Task Async-2 completed Task Async-3 completed Task Async-4 completed Task Async-5 completed## sample

sync
Task Sync-1 starting

Task Sync-1 completed Task Sync-2 starting Task Sync-2 completed Task Sync-3 starting Task Sync-3 completed Task Sync-4 starting Task Sync-4 completed Task Sync-5 starting Task Sync-5 completed

</p>