当前位置:网站首页>一道golang中关于recover的面试题
一道golang中关于recover的面试题
2022-07-25 20:48:00 【youngqqcn】
写出以下逻辑,要求每秒钟调用一次proc并保证程序不退出
package main
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
}()
select {
}
}
func proc() {
panic("ok")
}
请自行编码,完成算法。答案在文末
,
,
,
,
,
,
,
,
,
,
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
for {
t := time.NewTicker(time.Second)
select {
case <-t.C:
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("%s\n", r)
}
}()
proc()
}()
}
}
}()
select {
}
}
以下实现有什么问题:
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
defer func() {
if r := recover(); r != nil {
fmt.Printf("%s\n", r)
}
main()
}()
for {
t := time.NewTicker(time.Second)
select {
case <-t.C:
proc()
}
}
}()
select {
}
}
栈空间会越来越大,知道栈溢出,程序崩溃
为了验证,我们去掉定时器
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
defer func() {
if r := recover(); r != nil {
// fmt.Printf("%s\n", r)
}
main()
}()
proc()
}()
select {
}
}
运行程序,观察进程占用的内存空间,会越来越大,不到几秒钟,程序被系统杀死
$ ./solution26
已杀死
边栏推荐
- leetcode-919:完全二叉树插入器
- Step num problem
- Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
- MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
- Using the OAP aspect causes the controller to be called repeatedly
- Leetcode-6129: number of all 0 subarrays
- 网络协议:TCP Part2
- wokerman 自定义写入日志文件
- Brush questions with binary tree (4)
- Follow up of Arlo's thinking
猜你喜欢

Fanoutexchange switch code tutorial

程序的编译和运行

数据库清空表数据并让主键从1开始

Docker builds redis cluster

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released

LeetCode刷题——猜数字大小II#375#Medium

Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

Leetcode-79: word search
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]
随机推荐
Huatai Securities account opening process, is it safe to open an account on your mobile phone
JS scope and scope chain
Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
数据库清空表数据并让主键从1开始
JMeter - interface test
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
leetcode-79:单词搜索
Unity vs -- the default debugging in VS is to start rather than attach to unity debugging
[cloud native] use of Nacos taskmanager task management
Explain in detail the principle of MySQL master-slave replication "suggestions collection"
牛客-TOP101-BM37
Scan delete folder problems
Temperature and humidity environment monitoring system based on stm32
[leetcode] 28. Implement strstr ()
基于腾讯地图实现精准定位,实现微信小程序考勤打卡功能
毕业从事弱电3个月,我为什么会选择转行网络工程师
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
LeetCode通关:哈希表六连,这个还真有点简单
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
Open source SPL enhances mangodb computing