-1

Hi i want to build an application which listens to state changes of a gpio pin on a raspberry pi. How can i create a java application that never terminates but does nothing but listen. The listening is done by the handleGpioPinDigitalStateChangeEvent() from https://pi4j.com/1.2/usage.html

Now i’m using a empty while(true) loop in the main. I want to replace it to improve the performance because it’s running on an older raspberry pi.

Thank you guys!

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17

1 Answers1

0

One option might be, instead of having a empty while loop to run indefinitely, you can get user input using scanner inside the while like below,

Scanner scanner = new Scanner (System.in);
boolean run = true;
while(run) {  
String userIP = scanner.next();
if(userIP.equals("exit"))
  run = false;
}

The above cide will not run the loop indefinitely (which might have impacted on performance) instead run and waits for user input in console. I haven't done any performance comparison but logically should be better than infinite loop.

Balaji
  • 76
  • 3