Runner simulates a person running in the relay race.
(baton chan int)
| 31 | |
| 32 | // Runner simulates a person running in the relay race. |
| 33 | func Runner(baton chan int) { |
| 34 | var newRunner int |
| 35 | |
| 36 | // Wait to receive the baton. |
| 37 | runner := <-baton |
| 38 | |
| 39 | // Start running around the track. |
| 40 | fmt.Printf("Runner %d Running With Baton\n", runner) |
| 41 | |
| 42 | // New runner to the line. |
| 43 | if runner != 4 { |
| 44 | newRunner = runner + 1 |
| 45 | fmt.Printf("Runner %d To The Line\n", newRunner) |
| 46 | go Runner(baton) |
| 47 | } |
| 48 | |
| 49 | // Running around the track. |
| 50 | time.Sleep(100 * time.Millisecond) |
| 51 | |
| 52 | // Is the race over. |
| 53 | if runner == 4 { |
| 54 | fmt.Printf("Runner %d Finished, Race Over\n", runner) |
| 55 | wg.Done() |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // Exchange the baton for the next runner. |
| 60 | fmt.Printf("Runner %d Exchange With Runner %d\n", |
| 61 | runner, |
| 62 | newRunner) |
| 63 | |
| 64 | baton <- newRunner |
| 65 | } |