mirror of https://github.com/q191201771/naza
parent
67d32aba74
commit
6904ae65a5
@ -0,0 +1,21 @@
|
||||
// Copyright 2020, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/naza
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package defertaskthread
|
||||
|
||||
import "time"
|
||||
|
||||
type deferTaskThread struct {
|
||||
}
|
||||
|
||||
func (d *deferTaskThread) Go(deferMS int, task TaskFn, param ...interface{}) {
|
||||
go func() {
|
||||
time.Sleep(time.Duration(deferMS) * time.Millisecond)
|
||||
task(param...)
|
||||
}()
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
// Copyright 2020, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/naza
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package defertaskthread_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/q191201771/naza/pkg/nazalog"
|
||||
|
||||
"github.com/q191201771/naza/pkg/defertaskthread"
|
||||
)
|
||||
|
||||
func TestDeferTaskThread(t *testing.T) {
|
||||
d := defertaskthread.NewDeferTaskThread()
|
||||
for i := 0; i < 300; i += 50 {
|
||||
d.Go(i, func(param ...interface{}) {
|
||||
ii := param[0].(int)
|
||||
nazalog.Debugf("running %d", ii)
|
||||
}, i)
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
// Copyright 2020, Chef. All rights reserved.
|
||||
// https://github.com/q191201771/naza
|
||||
//
|
||||
// Use of this source code is governed by a MIT-style license
|
||||
// that can be found in the License file.
|
||||
//
|
||||
// Author: Chef (191201771@qq.com)
|
||||
|
||||
package defertaskthread
|
||||
|
||||
var thread DeferTaskThread
|
||||
|
||||
func Go(deferMS int, task TaskFn, param ...interface{}) {
|
||||
thread.Go(deferMS, task, param...)
|
||||
}
|
||||
|
||||
func init() {
|
||||
thread = NewDeferTaskThread()
|
||||
}
|
Loading…
Reference in New Issue