sanketmahore / JobSchedulingPOC

Job Scheduling POC done as an exercise to practice concurrency in Golang.
0 stars 1 forks source link

When will this go routine terminate ? #4

Open hariprasadraja opened 3 years ago

hariprasadraja commented 3 years ago

https://github.com/sanketmahore/JobSchedulingPOC/blob/de635437ff81946f47028309ab5ad8990435da2a/JobScheduling/jobs.go#L61-L67

sanketmahore commented 3 years ago

The for loop is creating n number of go routines, each go routine is reading from the channel. If there is no task to be executed present on the channel i.e channel is empty, then the go routine will terminate.

hariprasadraja commented 3 years ago

The for loop is creating n number of go routines, each go routine is reading from the channel. If there is no task to be executed present on the channel i.e channel is empty, then the go routine will terminate.

Well, It won't. try a small program with empty channel in range and observe it.

sanketmahore commented 3 years ago

The for loop is creating n number of go routines, each go routine is reading from the channel. If there is no task to be executed present on the channel i.e channel is empty, then the go routine will terminate.

This is happening because I am closing the JobList channel after adding all the tasks. So the channel accessed by the goroutines is a closed channel.

`
case FIFO:

    sort.Sort(ByArrivalTime(jobs))
    for _, job := range jobs {
        JobList <- job
    }
    close(JobList)
case LIFO:
    sort.Sort(ByArrivalTime(jobs))
    for i := len(jobs) - 1; i >= 0; i-- {
        JobList <- jobs[i]
    }
    close(JobList)
case PriorityQueue:
    sort.Sort(ByPriority(jobs))
    for _, job := range jobs {
        JobList <- job
    }
    close(JobList)

`

hariprasadraja commented 3 years ago

Agree. In the case of dynamic jobs, how can you sort the inputs. do you know the input jobs you have received are complete ?

What if It is not going to complete and you are going to get the inputs always like a web server.