当前位置: 首页 > news >正文

绿建设计院网站17做网站郑州

绿建设计院网站,17做网站郑州,茂名企业自助建站系统,wordpress主题tint-k设想一下你在接收源源不断的数据,如果有700ms没有收到,则认为是一个超时,需要做出处理。 逻辑上可以设计一个grouting,里面放一个通道,每收到一条数据进行相应处理。通道中夹杂一个timer定时器的处理,若通道在700ms内…

设想一下你在接收源源不断的数据,如果有700ms没有收到,则认为是一个超时,需要做出处理。

逻辑上可以设计一个grouting,里面放一个通道,每收到一条数据进行相应处理。通道中夹杂一个timer定时器的处理,若通道在700ms内有数据,则定时器被重置,重新等待700ms再调用定时器处理函数,否则,700ms时间到,运行定时器处理函数。示例代码如下:

​
package mainimport ("fmt""runtime/debug""strings""time"
)type demoSaveSuite struct {chanDemoSave chan string
}const dbSaveWaitDuration = 700 * time.Millisecondfunc (s *demoSaveSuite) DemoHandler() {go func() {defer func() {if r := recover(); r != nil {fmt.Println(string(debug.Stack()))}}()var timer *time.Timerfor sqlEntity := range s.chanDemoSave {fmt.Printf("Received SQL entity: %s\n", sqlEntity)// 检查 sqlEntity 是否包含 "SQL_Entity_5"if strings.Contains(sqlEntity, "SQL_Entity_5") {if timer != nil {timer.Stop()timer = nil // 将 timer 设为 nil 以确保不再使用它continue    // 跳过本次循环的后续部分}}if timer == nil {timer = time.AfterFunc(dbSaveWaitDuration, func() {fmt.Printf("Executing action after delay for entity: %s\n", sqlEntity)})} else {timer.Reset(dbSaveWaitDuration)}}}()
}func main() {// You can place your main code here if neededsuite := &demoSaveSuite{chanDemoSave: make(chan string, 5), // Buffer to avoid blocking when sending messages}go suite.DemoHandler()// Send the first SQL entitysuite.chanDemoSave <- "SQL_Entity_1_300Millisecond"time.Sleep(300 * time.Millisecond) // sleep a bit, but not enough for dbSaveWaitDuration// Send the second SQL entitysuite.chanDemoSave <- "SQL_Entity_2_800Millisecond"time.Sleep(800 * time.Millisecond) // let the timer expire for SQL_Entity_1// Send the third SQL entity to test resetting the timersuite.chanDemoSave <- "SQL_Entity_3_300Millisecond"time.Sleep(300 * time.Millisecond) // sleep a bit, but not enough for dbSaveWaitDuration// Send the fourth SQL entitysuite.chanDemoSave <- "SQL_Entity_4_800Millisecond"time.Sleep(800 * time.Millisecond) // let the timer expire for SQL_Entity_3// Send the fourth SQL entitysuite.chanDemoSave <- "SQL_Entity_5_2500Millisecond"time.Sleep(2500 * time.Millisecond) // let the timer expire for SQL_Entity_3// Close the channelclose(suite.chanDemoSave)
}​

代码动行结果:

Received SQL entity: SQL_Entity_1_300Millisecond
Received SQL entity: SQL_Entity_2_800Millisecond
Executing action after delay for entity: SQL_Entity_2_800Millisecond
Received SQL entity: SQL_Entity_3_900Millisecond
Received SQL entity: SQL_Entity_4_800Millisecond
Executing action after delay for entity: SQL_Entity_4_800Millisecond
Received SQL entity: SQL_Entity_5_2500Millisecond
Executing action after delay for entity: SQL_Entity_5_2500Millisecond

从运行结果看,只要超过700ms没有数据进入,就会引发定时器的回调,并且从2500ms的超时看只激发了一次,说明这里的定时器只会运行一次。没有超过700ms的,由于定时器被重置了,又开始等700ms才会运行,运行SQL_Entity_3时,定时器被删除,从结果看,虽然间隔是900ms远超700ms,依然定时器没有执行。接收到SQL_Entity_4时,由于timer已经为nil,因此又重新开启定时器

http://www.yayakq.cn/news/687495/

相关文章:

  • 微网站模板标签廊坊百度seo公司
  • 包做包装的网站wordpress is home
  • 网站管理方案微信分销是什么
  • 网站建设收费报价表织梦技术个人网站模板下载
  • 网站改版对网站优化影响最大的问题是什么网店营销模式
  • 站长工具是做什么的wordpress 登录用户
  • 网站制作厂家电话多少网站版面在线设计
  • 手机创建个人网站 免费怎样做网站的关键字搜索功能
  • 网站解决访问量超载福建省建设厅网站 登录
  • 南宁建站平台网页游戏网站有哪些
  • 临武县网站建设建设公积金网站
  • 高端网站设计合肥网站建设什么是企业云网站建设
  • 网站大图片优化盘县做会计兼职的网站
  • 做公司网站需要几个域名微信点餐小程序怎么制作
  • 建立公司网站需要注意什么开官网
  • 云南 网站模版广州免费核酸检测地点查询
  • wordpress网站ampwordpress无域名ip访问
  • 中贸网做的网站无锡做网站公司哪家好电话
  • 小型网站建设的经验搜索引擎关键词竞价排名
  • 郑州七彩网站建设公司 交通如何在抖音上投放广告
  • 学网站建设好吗网站设计高端
  • 免费做图片的网站网页制作类软件有哪些
  • 做网站的收获及感想定制开发小程序报价
  • 怎么查网站服务器博达网站建设教程
  • 科协网站建设建议网店推广联盟
  • 如何利用站群做网站高新区网站开发
  • 做ppt设计师哪个网站好做曖免费网站
  • 建设银行网站无法转账网站备案每年审吗
  • 电子商务网站建设软件网站建设周期表
  • 唐山市住房与城乡建设厅网站毕业设计网站开发流程