netty中使用Thread.sleep()方法的问题

本文探讨了一个在循环中使用Thread.sleep导致IO阻塞的问题,并通过调整代码顺序解决了该问题。文中详细记录了问题发现过程及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


    今天在项目测试过程中,为了达到每隔秒向服务器发送消息的目的,所以做了个循环并每100次等待3秒,代码为如下:

if(channel.isWritable()){
         for(int i = 0; i < testN; i++){
              if(i % 100 == 0){
                  Thread.sleep(1000);
              }future = channel.write(s);
         }
         future.addListener(ChannelFutureListener.CLOSE);}

     这样执行后,却发现在服务端,除了第一次数据能收到,以后的每次都不行,打印isWritable方法发现返回均为false,说明IO已经被阻塞,当前无法写入。甚是苦恼,百度谷歌后也没发现具体解决方法。
   后来偶尔一次,把sleep语句放在channel.write(s)后面,竟然每次的写入都可成功。虽然找到的解决方法,但为什么产生问题还不明了,希望哪位大神能来解惑,谢谢!
修改后再次调用此方法,控制台打印下面信息,请帮我分析一下:Message: androidx.test.uiautomator.StaleObjectException Stacktrace: io.appium.uiautomator2.common.exceptions.StaleElementReferenceException: androidx.test.uiautomator.StaleObjectException at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:45) at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:262) at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:256) at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) at java.lang.Thread.run(Thread.java:1024) Caused by: androidx.test.uiautomator.StaleObjectException at androidx.test.uiautomator.UiObject2.getAccessibilityNodeInfo(UiObject2.java:647) at androidx.test.uiautomator.UiObject2.hashCode(UiObject2.java:105) at java.lang.Object.toString(Object.java:299) at java.util.Formatter$FormatSpecifier.printString(Formatter.java:3138) at java.util.Formatter$FormatSpecifier.print(Formatter.java:3015) at java.util.Formatter.format(Formatter.java:2678) at java.util.Formatter.format(Formatter.java:2614) at java.lang.String.format(String.java:4017) at io.appium.uiautomator2.utils.ReflectionUtils.invoke(ReflectionUtils.java:84) at io.appium.uiautomator2.core.AxNodeInfoExtractor.extractAxNodeInfo(AxNodeInfoExtractor.java:51) at io.appium.uiautomator2.core.AxNodeInfoExtractor.toAxNodeInfo(AxNodeInfoExtractor.java:41) at io.appium.uiautomator2.utils.ElementHelpers.canSetProgress(ElementHelpers.java:77) at io.appium.uiautomator2.model.BaseElement.canSetProgress(BaseElement.java:160) at io.appium.uiautomator2.handler.SendKeysToElement.setProgress(SendKeysToElement.java:47) at io.appium.uiautomator2.handler.SendKeysToElement.safeHandle(SendKeysToElement.java:111) at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41) ... 33 more 调用的这个方法内容如下: def random_operation(self, duration,package_name,timeout=25): crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count = 0, 0, 0, 0, 0 # 获取设备屏幕尺寸 screen = self.driver.get_window_size() screen_width = screen['width'] screen_height = screen['height'] start_time = time.time() last_operation_time = time.time() # 记录最后一次成功操作的时间 while time.time() - start_time < duration: # 检测全局超时 if time.time() - last_operation_time > timeout: print(f"操作超时({timeout}秒无响应),结束测试") break #驻留检测 try: currentt_package = self.get_current_package().lower() target_package = package_name.lower() if currentt_package != target_package: print(f"当前包名不匹配目标!!!") self.driver.activate_app(package_name) time.sleep(3) last_operation_time = time.time() except Exception as e: print(f"驻留检测失败:{e}") try: # 随机选择操作类型(点击/滑动/输入) operation = random.choice(['click', 'swipe', 'input']) if operation == 'click': # 随机坐标点击 x = random.randint(0, screen_width) y = random.randint(0, screen_height) self.driver.tap([(x, y)], duration=50) time.sleep(0.3) elif operation == 'swipe': # 随机方向滑动(上下左右) direction = random.choice(['up', 'down', 'left', 'right']) if direction == 'up': self.driver.swipe(screen_width // 2, screen_height * 3 // 4, screen_width // 2, screen_height // 4, 500) elif direction == 'down': self.driver.swipe(screen_width // 2, screen_height // 4, screen_width // 2, screen_height * 3 // 4, 500) elif direction == 'left': self.driver.swipe(screen_width * 3 // 4, screen_height // 2, screen_width // 4, screen_height // 2, 500) elif direction == 'right': self.driver.swipe(screen_width // 4, screen_height // 2, screen_width * 3 // 4, screen_height // 2, 500) time.sleep(0.5) elif operation == 'input': # 查找输入框元素并随机输入数字 input_elements = self.driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.EditText") if input_elements: input_element = random.choice(input_elements) input_element.click() random_digits = ''.join(str(random.randint(0, 9)) for _ in range(random.randint(1, 10))) input_element.send_keys(random_digits) time.sleep(0.8) if self.check_app_crash(): crash_count += 1 if self.check_app_anr(): anr_count += 1 is_flower, is_black, is_white = self.verify_screen() if is_flower: flower_screen_count += 1 if is_black: black_screen_count += 1 if is_white: white_screen_count += 1 last_operation_time = time.time() except Exception as e: print(f"随机操作执行异常,异常信息:{e}") return (crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count)
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值