如何避免使用覆盆子轮询?

How to avoid to use polling with raspberry?

我正在 Java 中用 Raspberry 做一个项目,我使用 Pi4j 库。 我不会使用轮询,所以例如我使用此代码:

public class ListenGpioExample {

  public static void main(String args[]) throws InterruptedException {
    System.out.println("<--Pi4J--> GPIO Listen Example ... started.");

    // create gpio controller
    final GpioController gpio = GpioFactory.getInstance();

    // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
    final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

    // create and register gpio pin listener
    myButton.addListener(new GpioPinListenerDigital() {
        @Override
        public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
            // display pin state on console
            System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
        }

    });

    System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

    // keep program running until user aborts (CTRL-C)
    for (;;) {
        Thread.sleep(500);
    }

    // stop all GPIO activity/threads by shutting down the GPIO controller
    // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
    // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO controller        
}}

我的问题是: 事实上,无限循环仍然是一种轮询形式?

因为我不明白死循环到底有没有必要

不,这不是投票。此示例使用事件侦听器,一旦 GPIO Pin

中发生状态更改,该侦听器就会启动

正如您在 Pi4J library page 上看到的那样:

The listener implementation is based on GPIO hardware interrupts not state polling.

for(;;) 旨在使程序保持活动状态,直到用户将其停止(您可以在评论中阅读)