Closed ganeshs4 closed 3 years ago
Not sure if this same as - https://github.com/Pi4J/pi4j/issues/479 , looks different to me.
Looks like it's the Future<?> pulse(long duration, PinState pulseState);
which is causing the above behavior. Wouldn't the future task thread stop after the task has been completed?
If updated the code to do something like this instead of pulse it helps (I understand I could also use the blocking pulse call - haven't tried that though):
Before: Which was causing threads to keep increasing on every operation
garageControlOpenOrClosePin.pulse(2000, PinState.HIGH); //the control needs high followed by low pulse to operate
Changed it to:
garageControlOpenOrClosePin.high();
Thread.sleep(2000);
garageControlOpenOrClosePin.low();
Does it grow past 25 threads?
The underlying implementation is using a ScheduledExecutorService
with a MAX_THREADS_IN_POOL = 25;
If you don't like this many threads, you can implement your own ExecutorServiceFactory
and set it using this static method:
https://github.com/Pi4J/pi4j/blob/cd8051d02949dc13da47b4b177c7dd1a0bd6a16b/pi4j-core/src/main/java/com/pi4j/io/gpio/GpioFactory.java#L142-L151
Looks like it's the
Future<?> pulse(long duration, PinState pulseState);
which is causing the above behavior. Wouldn't the future task thread stop after the task has been completed?
Not necessarily. TASK != THREAD
From what I understand the thread pool may grow up to the maximum number of fixed threads allowed. The threads may remain around and IDLE for future re-use by the ScheduledExecutorService
.
This can have some adverse affects as noted in Issue #489.
We are proposing to change the implementation to a single thread in Pi4J version 1.3 and forward. This puts more responsibility on the user to handle event listeners promptly and not block other events in the pipeline waiting on the thread but should better keep events orderly and in sequence.
On my branch I have implemented a single threaded GPIO event dispatcher, thus resolving this issue. One can use it by setting the following:
import com.pi4j.concurrent.SingleThreadGpioExecutorServiceFactory;
...
GpioFactory.setExecutorServiceFactory(new SingleThreadGpioExecutorServiceFactory());
My branch is at https://github.com/eitch/pi4j/tree/develop/1.4
Closed. No further activity and v1.4 switches to a single thread GPIO event dispatcher.
I have a setup which controls garage door based on the input received (open/close) from a web interface. The code is something like below:
The above is executed as below:
Whenever the above is executed, there are 2 additional thread added to the JVM which are never terminated. The 2 new thread added are in WAITING state. Every time the garage door is commanded to be opened it adds 2 threads to WAITING state, same when close command is issued. I captured thread dumps and see below which seems to be getting added but never terminates (seems like thread pool executor - pi4j-scheduled-executor-x):
This keeps growing. I even tried singleton using ENUM but it's the same behavior. Any clue, how to handle this?