简体   繁体   English

while循环还是线程?

[英]While-loop or threads?

I'm currently programming on a Lego NXT 2.0 with leJOS 0.9.1 beta firmware. 我目前正在使用leJOS 0.9.1 beta固件编程Lego NXT 2.0

I have several different sensors, like a color sensor and a ultrasound sensor. 我有几种不同的传感器,如颜色传感器和超声波传感器。

I'm using a while-loop to make the robot drive forward, until it hits a wall. 我正在使用while循环让机器人向前行驶,直到撞到墙壁。 However, for some reason, I don't like this approach and wanted to implement a listener instead. 但是,出于某种原因,我不喜欢这种方法,而是希望实现一个监听器。 A leJOS developer wrote, however, that using the listener model is not recommended and I should use threads instead to poll the value of the ultrasound sensor. 然而,leJOS开发人员写道,不推荐使用监听器模型,我应该使用线程来轮询超声波传感器的值。

Now I'm wondering how bad the implementation with a while-loop really is (operating system wise, as in wasting of resources) and how a threading model would be more beneficial (and implemented)? 现在我想知道while循环的实现有多糟糕(操作系统明智,如浪费资源)以及线程模型如何更有益(并实现)?

MWE: MWE:

public class SensorTest {
    static UltrasonicSensor sonic;
    static DifferentialPilot pilot;
    public static final int DISTANCE = 20;

    public static void main(String[] args){
        sonic = new UltrasonicSensor(SensorPort.S1);
        pilot = new DifferentialPilot(8.5, 25, Motor.C, Motor.B);
        int i = 0;      

        while (i < DISTANCE) {
            pilot.forward();
            i = sonic.getDistance();
        }
        pilot.stop();
      }
}

Not having any experience wit the Lego blocks (I'm serious jealous) you have to ask yourself some serious questions. 乐高积木没有任何经验(我非常嫉妒),你必须问自己一些严肃的问题。

In a simple situation, using a loop in this manner may not be a bad solution, but as you add more to, the time it takes to process each request (check n number of sensors, perform n number of operations), it will become slower to react. 在一个简单的情况下,以这种方式使用循环可能不是一个糟糕的解决方案,但随着您添加更多,处理每个请求所需的时间(检查n个传感器,执行n个操作),它将成为反应迟钝。

A Threaded model may allow you to process more data, but you may need to consider how many threads you need and what they are doing. Threaded模型可能允许您处理更多数据,但您可能需要考虑您需要多少线程以及它们正在做什么。

For example, you could have a Driver thread, whose sole responsibility is to move the Lego. 例如,您可以拥有一个Driver线程,其唯一责任是移动Lego。 It would do this be querying a number of "sensor" thread's to determine what it is telling, but each sensor thread would be polling data at there own rate and not caring about anything else. 这样做会查询一些“传感器”线程以确定它所说的内容,但每个传感器线程都会以自己的速率轮询数据而不关心其他任何事情。

You could also inverse the model, allow the Driver to continue moving until a Sensor thread raised some kind of alert and told the Driver it should change directions. 您也可以反转模型,允许Driver继续移动,直到Sensor线程发出某种警报并告诉Driver它应该改变方向。

I, personally, would be trying to see if I could get a thread model working (even for a simple device) as it will provide more flexibility into the future to expand the operations... 我个人会试图看看我是否可以使用一个线程模型(即使对于一个简单的设备),因为它将为未来扩展操作提供更大的灵活性......

At the end of the day, it's going to be balancing act 在一天结束时,它将是平衡行为

Seriously jealous! 真的很嫉妒!

Now I'm wondering how bad the implementation with a while-loop really is (operating system wise, as in wasting of resources) and how a threading model would be more beneficial (and implemented)? 现在我想知道while循环的实现有多糟糕(操作系统明智,如浪费资源)以及线程模型如何更有益(并实现)?

The 'threading model' is a while loop, executed on some thread other than the main thread. '线程模型'是一个while循环,在主线程以外的某个线程上执行。 If you're concerned about resource consumption, you can have the thread go to sleep for some fixed interval before it polls for a value again. 如果您担心资源消耗,您可以让线程在再次轮询值之前进入休眠状态一段固定时间。

As far as how this is implemented, you'll have to read through the API , but in a standard java program, if you're using a newer JDK, you just create a class that implements the Runnable interface, and pass this object to a thread executor. 至于如何实现它,你必须阅读API ,但在标准的java程序中,如果你使用的是更新的JDK,你只需要创建一个实现Runnable接口的类,并将该对象传递给线程执行器。

Using threading vs. polling is dependent on your situation. 使用线程与轮询取决于您的情况。 I suppose a thread would act more like an interrupt when programming microcontrollers. 我想在编程微控制器时,一个线程更像是一个中断。 With a thread (or interrupt) you can better modularize your code, make it more efficient. 使用线程(或中断),您可以更好地模块化代码,使其更有效。 See this thread- 看到这个线程 -

Polling or Interrupt based method 基于轮询或中断的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM