Files
queue/examples/example_parallel_test.go
Кобелев Андрей Андреевич 557fb40268 Change package path
2025-10-14 12:53:46 +05:00

38 lines
672 B
Go

package queue_test
import (
"context"
"fmt"
"sync"
"time"
"git.belvedersky.ru/queue"
)
// Example_parallel демонстрирует работу нескольких воркеров.
func Example_parallel() {
type Task struct{ ID int }
q := queue.NewQueue[Task](5)
var mu sync.Mutex
q.Register(func(t Task) {
mu.Lock()
fmt.Printf("Worker processed: %d\n", t.ID)
mu.Unlock()
time.Sleep(20 * time.Millisecond)
})
ctx := context.Background()
q.HandleParallel(ctx, 4)
for i := range 3 {
q.Produce(Task{ID: i})
}
q.Shutdown(context.Background())
// Unordered output:
// Worker processed: 0
// Worker processed: 1
// Worker processed: 2
}