CPSC270A
Software Eng & Project Design

Activity 5

Producer Consumer with Dart

Write a dart progam that simulates a producer and consumer.

Details

The program should have two functions, produce and consume that run simultaniously adding to and removing from a shared list of ints.

  1. The program should have a global list of ints variable.

  2. The produce function should periodically add to the list of ints. Each add should be delayed from the previous add by some duration of seconds. Each time it adds to the list, it should print what was added. The function should stop when it has added some fixed number of ints.

  3. The consume function should periodically attempt to remove an int from the the list of ints. If the list is empty, the consume function should do nothing. Each attempt to remove should be delayed from the previous attempt to remove by some duration of seconds that is longer than the produce function’s delay. Each time it attempts to remove from the list, it should print what was removed or whether the list is empty. The function should stop when it has attempted to remove a fixed number of ints.

  4. The main function should not end until both the produce and consume functions have finished.

Example

producer: added 0
list: [0]

producer: added 1
list: [0, 1]

consumer: removed 0
list: [1]

producer: added 2
list: [1, 2]

consumer: removed 1
list: [2]

consumer: removed 2
list: []

consumer: can not remove
list: []