停止不被中断的线程(stop thread that does not get interrupted)
我有一个坐在ObjectInputStream中并从中读取对象的线程:
public void run() { try { ois = new ObjectInputStream(clientSocket.getInputStream()); Object o; while ((o = ois.readObject()) != null) { //do something with object } } catch (Exception ex) { //Log exception } }
readObject不会抛出InterruptedException,据我所知,当此线程被中断时不会抛出异常。 我该如何阻止这个帖子?
I have a thread that sits and reads objects off of an ObjectInputStream:
public void run() { try { ois = new ObjectInputStream(clientSocket.getInputStream()); Object o; while ((o = ois.readObject()) != null) { //do something with object } } catch (Exception ex) { //Log exception } }
readObject does not throw InterruptedException and as far as I can tell, no exception is thrown when this thread is interrupted. How do I stop this thread?
原文:https://stackoverflow.com/questions/3046996
最满意答案
//For toggle the LED for certain length of time use the delay() //call delay(UNIT) to pause execution of UNIT milliseconds //long unit *3 , short = unit void displayMorseCode(byte* msg, int len) { int delayT = 0; int unit = 300; // TODO :Interpret the message toggle LED on and off to display the morse code for (int i = 0 ; i< len; i++) { if (msg[i] == 1) { digitalWrite(LED_PIN,HIGH); delayT = 3*unit; } else { digitalWrite(LED_PIN,LOW); delayT = unit; } delay(delayT); }
这是一个非常简单的答案,它将根据收到的字节更改持续时间。 现在你必须为每个字节创建一个字典,这样根据字节(即S = short,short,short)你创建写输出,就像我给你看的那样,但是你应该用new更改digitalWrite()功能,将创建每个字母的摩尔斯电码。 所以if条件是每个字母(即
if (msg[i] == 83)
- 十进制ASCII中的S-)//For toggle the LED for certain length of time use the delay() //call delay(UNIT) to pause execution of UNIT milliseconds //long unit *3 , short = unit void displayMorseCode(byte* msg, int len) { int delayT = 0; int unit = 300; // TODO :Interpret the message toggle LED on and off to display the morse code for (int i = 0 ; i< len; i++) { if (msg[i] == 1) { digitalWrite(LED_PIN,HIGH); delayT = 3*unit; } else { digitalWrite(LED_PIN,LOW); delayT = unit; } delay(delayT); }
This is a very simple answer, that will change the duration depending on the byte received. Now you must create a dictionary for each byte, so that depending on the byte (i.e. S = short,short,short) you create the write output, in the same way I showed you, but you should change digitalWrite() with the new function, that will create each letter's morse code. So the if condition would be for each letter (i.e.
if (msg[i] == 83)
- the S in decimal ASCII-)
相关问答
更多-
打开Arduino LED(Turning On Arduino LED)[2023-09-29]
您可能需要考虑几件事情。 第一个是serialEvent()函数。 只要数据到达相应的串口,就会调用此函数(对于arduino Mega和Due还有其他函数,如serialEvent1() , serialEvent2()等等......) 这将是你的arduino的代码: void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read(); ... -
我看到你的代码有两个主要问题: 您正在使用Serial ,尽管您正在为您的LED灯带预留引脚 1 。 您不能同时使用两种方法:要么将引脚用于串行接口(默认情况下使用引脚0和1 ),要么使用引脚作为引脚。 尝试做这两件事可能会损坏您的组件和/或您的设备。 因此,您应该删除所有引用Serial接口的代码。 您没有将任何引脚设置为LOW状态,这意味着如果在一个循环中数字等于1023 ,那么在此之后每个引脚将永远处于HIGH 。 您应该在循环开始时将每个引脚设置回LOW ,或者为每个if条件添加一个else分支。 ...
-
我在初始化时看到一个问题,在这一行: POT = Arduino.analogRead(0) 然后,在这里,你在这里使用POT作为引脚号,但POT将在上面的行中初始化为变量: pot = Arduino.analogRead(POT) 我认为这就是你出乎意料的行为的原因。 我认为如果你将POT的初始化改为POT=0或你的电位器连接的引脚号(如果它不是引脚0),它可能会起作用。 I figured it out. The blink issue was not expected. However it w ...
-
看起来你的'else'语句属于错误的'if'块。 根据你的目标,你应该拥有它,这样如果没有按下一个键,你就会把Arduino.LOW写到9-12针。 基本上,只需移动一个支架: void draw() { if (keyPressed == true) { if (key == 'w' || key == 'W') { arduino.digitalWrite (12, Arduino.HIGH); } if (key == 's' || key == 'S') { ...
-
Arduino LED闪烁问题(Arduino LED blinking issue)[2022-11-09]
您需要将主循环更改为: void loop() { if (Serial.available()> 0){ LED = Serial.read(); Serial.print(LED); } if (LED == '3') { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); } ... -
非常简单的疏忽。 这条线: for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { digitalWrite(eyespin, fadeValue); 应该写: for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { analogWrite(eyespin, fadeValue); 注意它现在的analogWrite而不是digitalWrite。 数字写入只能产生值0 ...
-
有很多问题,例如: leds[0] = CRGB(36, 0, 0); 意即: 将参数复制到堆栈 调用CRBG功能 创建本地LED对象 通过调用约束来调用该局部对象上的setR setG ... setB ... 返回本地对象的副本 led [0]上的复制分配运算符 对12b颜色使用8b宽变量也有点多余。 所以一开始我会推荐这样的东西: class LED { public: uint16_t rgb; LED(uint8_t r=0, uint8_t g=0, uint8_t b= ...
-
唯一显而易见的是你有一个状态标志,用于增加值的方式,但是你没有在第一个if中测试它。 最好再构建一下代码。 如果你有多个引脚,你也可能想跟踪多个值,除非它们都应该同时淡入和淡出。 在这种情况下,你最好使用一组带有每个松树参数的struct 。 使用具有多个任务的延迟的一种方法是使每个任务在从上一个循环开始经过的时间内工作,并在循环结束时调整任务所花费的时间的延迟。 如果你的循环是这样的: static unsigned long last_time = 0; // set to millis() in se ...
-
如何使用Arduino UNO和按钮使电路上的LED闪烁?(How to blink an LED on a circuit using an Arduino UNO and a button?)[2021-09-12]
您必须将LED连接到Arduino的PWM输出之一。 PWM输出可以设置为0到255之间的值,这意味着与将该电流设置为该输出的值成比例的时间,其余时间将为0 V. 请查看Arduino官方网站上的以下示例,以淡化LED 。 您还应该使用函数映射 ,因为它可以简化代码映射值。 至于你的代码,你可以尝试这个(我没有编译它,所以原谅任何错误): // Read the state of the pushbutton value, and update the fade LED value: buttonState ... -
//For toggle the LED for certain length of time use the delay() //call delay(UNIT) to pause execution of UNIT milliseconds //long unit *3 , short = unit void displayMorseCode(byte* msg, int len) { int delayT = 0; int unit = 300; // TODO :Interpret the m ...