mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -40,6 +40,34 @@ func New(f func()) {
 | 
			
		||||
	}()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewWithArgs 创建带有参数的goroutine
 | 
			
		||||
func NewWithArgs(f func(args ...interface{}), args ...interface{}) {
 | 
			
		||||
	_, file, line, _ := runtime.Caller(1)
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		locker.Lock()
 | 
			
		||||
		instanceId++
 | 
			
		||||
 | 
			
		||||
		var instance = &Instance{
 | 
			
		||||
			Id:          instanceId,
 | 
			
		||||
			CreatedTime: time.Now(),
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		instance.File = file
 | 
			
		||||
		instance.Line = line
 | 
			
		||||
 | 
			
		||||
		instanceMap[instanceId] = instance
 | 
			
		||||
		locker.Unlock()
 | 
			
		||||
 | 
			
		||||
		// run function
 | 
			
		||||
		f(args...)
 | 
			
		||||
 | 
			
		||||
		locker.Lock()
 | 
			
		||||
		delete(instanceMap, instanceId)
 | 
			
		||||
		locker.Unlock()
 | 
			
		||||
	}()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// List 列出所有正在运行goroutine
 | 
			
		||||
func List() []*Instance {
 | 
			
		||||
	locker.Lock()
 | 
			
		||||
 
 | 
			
		||||
@@ -19,3 +19,10 @@ func TestNew(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	time.Sleep(1 * time.Second)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestNewWithArgs(t *testing.T) {
 | 
			
		||||
	NewWithArgs(func(args ...interface{}) {
 | 
			
		||||
		t.Log(args[0], args[1])
 | 
			
		||||
	}, 1, 2)
 | 
			
		||||
	time.Sleep(1 * time.Second)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user