func main() { defer fmt.Println("Main defer") go t1() go t2() go t3() time.Sleep(20 * time.Second)}func t1() { defer fmt.Println("t1 defer") time.Sleep(20 * time.Second)}func t2() { defer fmt.Println("t2 defer") time.Sleep(5 * time.Second) panic(New("T2"))}func t3() { defer fmt.Println("t3 defer") time.Sleep(20 * time.Second)}1个线程(t2)调用发生恐慌,t2延迟被调用。当t2出现紧急情况时,所有其他线程也将终止。我希望每个线程的延迟都被调用。在这种情况下,必须紧急处理,而紧急处理在一个线程中。因此,我希望每个线程都知道程序将要退出。有什么办法可以实现?当前输出:t2 deferpanic: T2goroutine 19 [running]:main.t2() C:/Users/Talha.Irfan/OneDrive - Bentley Systems, Inc/Desktop/go_test/src/main2/main.go:34 +0x105created by main.main C:/Users/Talha.Irfan/OneDrive - Bentley Systems, Inc/Desktop/go_test/src/main2/main.go:21 +0xb0
1 回答
- 1 回答
- 0 关注
- 241 浏览
添加回答
举报
0/150
提交
取消