Пример #1
0
func main() {
	stop := make(chan bool)

	// Create a channel to put task
	ch := make(chan tq.Task)

	// Create a new task queue
	q := tq.NewTaskQueue(ch)

	// Run this task queue in a separate goroutine
	go q.Run()

	// We create 5 tasks.
	// Every second, there will be one running task.
	for i := 0; i < 5; i++ {
		j := new(MyJob)
		j.id = i
		j.ch = stop

		// We need two parameters: the Runnable interface,
		// and the channel communicates with task queue.
		t := tq.NewCommonTask(j, ch)

		// Set the running time.
		// The task with id i will be run after i + 1 seconds.
		t.After(int64(i) + 1)
		ch <- t
	}
	for i := 0; i < 5; i++ {
		<-stop
	}
}
Пример #2
0
func main() {
	stop := make(chan bool)

	// Create a channel to put task
	ch := make(chan tq.Task)

	// Create a new task queue
	q := tq.NewTaskQueue(ch)

	// Run this task queue in a separate goroutine
	go q.Run()
	j := new(MyJob)
	j.ch = stop

	t := tq.NewCommonTask(j, ch)

	// The task will be executed after 2 seconds
	t.After(2)

	ch <- t
	<-stop
}