-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adiciona explicações e exemplos sobre sistema de ticket
- Loading branch information
1 parent
e31f5c8
commit a076436
Showing
2 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type ( | ||
Trabalho func() | ||
ticket int | ||
) | ||
|
||
func trabalhador(tickets <-chan ticket, work <-chan Trabalho) { | ||
for w := range work { | ||
<-tickets // espera por um ticket | ||
w() // executa um trabalho | ||
} | ||
} | ||
|
||
func bilheteria(tickets chan<- ticket, timeout time.Duration, nTickets int) { | ||
for { | ||
for i := 0; i < nTickets; i++ { | ||
tickets <- ticket(i) | ||
} | ||
|
||
// espera até que mais tickets possam ser emitidos | ||
<-time.After(timeout) | ||
} | ||
} | ||
|
||
func main() { | ||
tickets := make(chan ticket) | ||
trabalhos := make(chan Trabalho) | ||
|
||
go bilheteria(tickets, 1*time.Second, 10) | ||
go trabalhador(tickets, trabalhos) | ||
|
||
for i := 0; i <= 30; i++ { | ||
|
||
trabalhos <- func() { | ||
fmt.Println("processando ticket") | ||
} | ||
fmt.Println("trabalho ", i, " enviado") | ||
} | ||
|
||
close(trabalhos) | ||
close(tickets) | ||
} |