0

for go language : can anyone tell me how the starting job is printing without printing finished job exactly after that

package main import ( "fmt" "time" ) func worker(id int, jobs <-chan int, results chan<- int) { for j := range jobs { fmt.Println("worker", id, "started job", j) time.Sleep(time.Second) fmt.Println("worker", id, "finished job", j) results <- j * 2 } } func main() { const numJobs = 5 jobs := make(chan int, numJobs) results := make(chan int, numJobs) for w := 3; w >= 1; w-- { go worker(w, jobs, results) } for j := 1; j <= numJobs; j++ { jobs <- j } close(jobs) for a := 1; a <= numJobs; a++ { <-results } } // Output : worker 1 started job 1 worker 3 started job 2 worker 2 started job 3 worker 1 finished job 1 worker 1 started job 4 worker 2 finished job 3 worker 2 started job 5 worker 3 finished job 2 worker 1 finished job 4 worker 2 finished job 5 i understand nearly all the problem how its working but just the part that the started & finished job are not printing consecutively

22nd Apr 2020, 11:08 AM
Sahil Bhakat
Sahil Bhakat - avatar
1 Respuesta
0
i have got the answer but are anyone else is following this question
5th May 2020, 11:26 AM
Sahil Bhakat
Sahil Bhakat - avatar