当前位置:网站首页>线程的阻塞问题
线程的阻塞问题
2022-06-24 06:44:00 【ligang2255】
Thread.join是个捣蛋的功能,事实上所有的阻塞都是捣蛋。
只有特殊的底层功能才需要阻塞,比如socket服务等,其他用户级的阻塞功能都是自找麻烦。
解决办法就是共用一个消息(全局变量),在一个可以接受的频率下去查看消息,事实上windows底层的消息循环机制本身就是一个反复查看,根本不存在什么所谓通知,而是有人负重前行,在底层不断查看消息,你在上层才感觉不到查看过程,还以为消息是自己过来的,根本就是有底层代码检查搬运。
所以,用户应用系统也可以设计一个消息机制,借用底层消息系统也可以,不借用更简单。
举个例子:
写个http服务,服务启动后,线程进入listen阻塞状态,此时保留soket为外部变量,线程本身循环检查退出条件,如果条件满足,socket.stop(),如果外部调用stop()也可以终止。
反而是Thread.Abort()并不能中断其内部的soket阻塞,也就不可能在其异常处理中执行soket.stop()。这是个逻辑错误,上层代码不能控制底层。最好的办法反而是最简单的,把阻塞的那个东西放在外部,直接stop,就会停止阻塞,同时也引发了上层调用的异常,线程于是可以正常退出,而不需要使用Abort。
边栏推荐
- 后疫情时代下,家庭服务机器人行业才刚启航
- 6000多万铲屎官,捧得出一个国产主粮的春天吗?
- Deploy L2TP in VPN (medium)
- [MySQL usage Script] clone data tables, save query data to data tables, and create temporary tables
- Spark stage and shuffle for daily data processing
- How to connect the Bluetooth headset to the computer and how to connect the win10 computer to the Bluetooth headset
- Anaconda 中使用 You Get
- The seminar on "global IPv6 development and outlook 2020-2021" was held in Beijing
- 《canvas》之第2章 直线图形
- Reppoints: Microsoft skillfully uses deformation convolution to generate point sets for target detection, full of creativity | iccv 2019
猜你喜欢
MaxCompute远程连接,上传、下载数据文件操作
Alibaba cloud full link data governance
get_ started_ 3dsctf_ two thousand and sixteen
bjdctf_2020_babystack
[image fusion] multi focus and multi spectral image fusion based on pixel saliency and wavelet transform with matlab code
Camera calibration (calibration purpose and principle)
爬虫基础B1——Scrapy(B站学习笔记)
MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics
Dichotomous special training
[image segmentation] retinal vessel segmentation based on morphology with matlab code
随机推荐
[learn FPGA programming from scratch -42]: Vision - technological evolution of chip design in the "post Moorish era" - 1 - current situation
MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics
随机数备注
【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
Black box and white box models for interpretable AI
any类备注
Spark stage and shuffle for daily data processing
UE common console commands
Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
L2TP connection failure guide in VPN
pair类备注
[机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
timer使用备注
Only two lines are displayed, and the excess part is displayed with Ellipsis
Actual target shooting - skillfully use SMB to take down the off-line host
How to delete / select an input method on your computer
PCL point cloud random sampling by ratio
Camera calibration (calibration purpose and principle)
What is automated testing? What software projects are suitable for automated testing?
Continue to have a fever. Try the asynchronous operation of dart language. The efficiency is increased by 500%