54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
|
|
package task
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"fmt"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// DelayTask 是一个用于模拟延迟的 Task 实现
|
|||
|
|
type DelayTask struct {
|
|||
|
|
id string
|
|||
|
|
duration time.Duration
|
|||
|
|
priority int
|
|||
|
|
done bool
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewDelayTask 创建一个新的 DelayTask 实例
|
|||
|
|
func NewDelayTask(id string, duration time.Duration, priority int) *DelayTask {
|
|||
|
|
return &DelayTask{
|
|||
|
|
id: id,
|
|||
|
|
duration: duration,
|
|||
|
|
priority: priority,
|
|||
|
|
done: false,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Execute 执行延迟任务,等待指定的时间
|
|||
|
|
func (d *DelayTask) Execute() error {
|
|||
|
|
fmt.Printf("任务 %s (%s): 开始延迟 %s...\n", d.id, d.GetDescription(), d.duration)
|
|||
|
|
time.Sleep(d.duration)
|
|||
|
|
fmt.Printf("任务 %s (%s): 延迟结束。\n", d.id, d.GetDescription())
|
|||
|
|
d.done = true
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetID 获取任务ID
|
|||
|
|
func (d *DelayTask) GetID() string {
|
|||
|
|
return d.id
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetPriority 获取任务优先级
|
|||
|
|
func (d *DelayTask) GetPriority() int {
|
|||
|
|
return d.priority
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IsDone 检查任务是否已完成
|
|||
|
|
func (d *DelayTask) IsDone() bool {
|
|||
|
|
return d.done
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetDescription 获取任务说明,根据任务ID和延迟时间生成
|
|||
|
|
func (d *DelayTask) GetDescription() string {
|
|||
|
|
return fmt.Sprintf("延迟任务,ID: %s,延迟时间: %s", d.id, d.duration)
|
|||
|
|
}
|