Gosched

Add code in line A to assure that the lowercase letters and capital letters are printed consecutively.

  1. package main
  2. import (
  3. "fmt"
  4. "runtime"
  5. "sync"
  6. )
  7. const N = 26
  8. func main() {
  9. const GOMAXPROCS = 1
  10. runtime.GOMAXPROCS(GOMAXPROCS)
  11. var wg sync.WaitGroup
  12. wg.Add(2 * N)
  13. for i := 0; i < N; i++ {
  14. go func(i int) {
  15. defer wg.Done()
  16. // A
  17. fmt.Printf("%c", 'a'+i)
  18. }(i)
  19. go func(i int) {
  20. defer wg.Done()
  21. fmt.Printf("%c", 'A'+i)
  22. }(i)
  23. }
  24. wg.Wait()
  25. }

Answer

  1. package main
  2. import (
  3. "fmt"
  4. "runtime"
  5. "sync"
  6. )
  7. const N = 26
  8. func main() {
  9. const GOMAXPROCS = 1
  10. runtime.GOMAXPROCS(GOMAXPROCS)
  11. var wg sync.WaitGroup
  12. wg.Add(2 * N)
  13. for i := 0; i < N; i++ {
  14. go func(i int) {
  15. defer wg.Done()
  16. runtime.Gosched()
  17. fmt.Printf("%c", 'a'+i)
  18. }(i)
  19. go func(i int) {
  20. defer wg.Done()
  21. fmt.Printf("%c", 'A'+i)
  22. }(i)
  23. }
  24. wg.Wait()
  25. }