首页 \ 问答 \ 停止不被中断的线程(stop thread that does not get interrupted)

停止不被中断的线程(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
更新时间:2023-05-02 07:05

最满意答案

//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-)

相关问答

更多
  • 您可能需要考虑几件事情。 第一个是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') { ...
  • 您需要将主循环更改为: 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 ...
  • 您必须将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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。