面试题答案
一键面试优化思路
- 复用Timer:避免频繁创建和销毁
time.Timer
,使用time.Timer
池来复用已有的Timer
实例。 - 及时停止Timer:确保在不再需要
Timer
时,及时调用Stop
方法停止定时器,避免其继续运行造成资源浪费。 - 优化调度逻辑:合理安排定时器触发的任务,减少不必要的定时器使用。
关键代码示例
package main
import (
"container/list"
"fmt"
"sync"
"time"
)
// TimerPool 定义定时器池
type TimerPool struct {
sync.Mutex
pool *list.List
}
// NewTimerPool 创建新的定时器池
func NewTimerPool() *TimerPool {
return &TimerPool{
pool: list.New(),
}
}
// Get 从池中获取一个定时器
func (tp *TimerPool) Get() *time.Timer {
tp.Lock()
defer tp.Unlock()
if tp.pool.Len() > 0 {
e := tp.pool.Front()
t := e.Value.(*time.Timer)
tp.pool.Remove(e)
return t
}
return time.NewTimer(0)
}
// Put 将定时器放回池中
func (tp *TimerPool) Put(t *time.Timer) {
tp.Lock()
defer tp.Unlock()
t.Reset(0)
tp.pool.PushBack(t)
}
func main() {
timerPool := NewTimerPool()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
timer := timerPool.Get()
select {
case <-timer.C:
case <-time.After(2 * time.Second):
}
timerPool.Put(timer)
}()
}
wg.Wait()
}
在上述代码中:
- 定义了
TimerPool
结构体用于管理time.Timer
的池,使用sync.Mutex
进行并发安全控制。 NewTimerPool
函数用于初始化定时器池。Get
方法从池中获取一个time.Timer
,如果池中没有则创建新的。Put
方法将time.Timer
放回池中,并重置定时器。- 在
main
函数中模拟了高并发场景下定时器的获取和放回操作。