This Go program demonstrates a simple task processing workflow using goroutines and channels. It initializes a task queue, creates multiple worker goroutines to process tasks concurrently, sends a specified number of tasks to the workers, and waits for all tasks to be completed before printing a confirmation message. The use of a WaitGroup
ensures that the main program waits for all workers to finish processing before exiting.
- ⚡ Utilizes goroutines for concurrent task processing.
- 📌 Implements channels for efficient task distribution.
- 🔄 Uses sync.WaitGroup to synchronize worker completion.
- ⏳ Simulates real-world task processing with time.Sleep.
package main
import (
"fmt"
"sync"
"time"
)
func worker(workerId int, tasksChannel chan int, waitGroup *sync.WaitGroup) {
defer waitGroup.Done()
for taskId := range tasksChannel {
executeTask(workerId, taskId)
}
}
func executeTask(workerId int, taskId int) {
fmt.Printf("Worker %d processing task %d\n", workerId, taskId)
time.Sleep(time.Second)
}
func main() {
const totalWorkers = 3
const totalRequestsAllowed = 10
tasksChannel := make(chan int, totalRequestsAllowed)
var waitGroup sync.WaitGroup
for workerIndex := 1; workerIndex <= totalWorkers; workerIndex++ {
waitGroup.Add(1)
go worker(workerIndex, tasksChannel, &waitGroup)
}
for taskIndex := 1; taskIndex <= totalRequestsAllowed; taskIndex++ {
tasksChannel <- taskIndex
}
close(tasksChannel)
waitGroup.Wait()
fmt.Println("All tasks processed.")
}
- ⚙️ Goroutines: The main program creates a pool of workers (goroutines), each of which processes tasks from the channel.
- 📦 Task Distribution: Tasks are distributed across the workers through the channel, processed in parallel.
- 🛠️ Synchronization:
sync.WaitGroup
ensures the program waits for all workers to finish before exiting.
- 📌 Initialize Task Queue: Create a buffered channel to hold tasks.
- 🚀 Create Workers: Launch
totalWorkers
worker goroutines. - 📤 Send Tasks: Send
totalRequestsAllowed
tasks to the channel. - 🔒 Close the Channel: Indicate that no more tasks will be added.
- ⚡ Workers Process Tasks: Workers read from the channel and process tasks concurrently.
- ⏳ Wait for Completion: The program waits for all workers to finish.
- ✅ Final Message: A confirmation message is printed after all tasks are processed.
- 🏢 Business Efficiency: Helps optimize task processing, reducing bottlenecks in concurrent workflows.
- 💡 Scalability: Can be extended to handle a higher number of tasks with minimal modifications.
- ⏳ Time Optimization: Reduces processing time by leveraging concurrent workers.
- 🚀 Practical Use Cases: Ideal for job queues, background processing, and distributed task execution.
Ensure you have Go installed on your machine. You can verify this with:
$ go version
To execute the program, run:
$ go run main.go