Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Eclipse SmartHome » Is it ok to use Thread.sleep() in binding(What if the binding needs to pause while waiting for device to respond ?)
Is it ok to use Thread.sleep() in binding [message #1759275] Sun, 09 April 2017 20:27 Go to next message
Charles Brouwers is currently offline Charles BrouwersFriend
Messages: 7
Registered: April 2017
Junior Member
My binding communicates with the device through Telnet. It takes several hundred milliseconds for a response to come back after the binding sends a command to the device. I have a Thread.sleep(300) after each send and everything works well. I was wondering however if this was an issue for the broader system ? The sleep instruction is either executed in the context of responding to a channel command or in the context of a recurring polling task. Is there a better way? should I create a separate thread instead and have instructions executed asynchronously ? Is there another solution ?
Re: Is it ok to use Thread.sleep() in binding [message #1759310 is a reply to message #1759275] Mon, 10 April 2017 13:05 Go to previous message
Simon Kaufmann is currently offline Simon KaufmannFriend
Messages: 51
Registered: January 2011
Member
This is not a problem at all if you are doing it in the recurring polling task, as it is running in its own Thread.

I'm a little more concerned about the handleCommand(), because there you will (unnecessarily) block the main Thread. The ThingHandler's methods which are called by the framework generally are expected to return "as fast as possible". While this is a rather soft criteria, sleeping certainly does not meet this requirement.

Therefore I would suggest to handle such communication in a separate thread, especially since the handleCommand() is not expected to return any results directly. So it is okay to do it asynchronously. Just: please use the scheduler and don't spawn the Thread yourself.
Re: Is it ok to use Thread.sleep() in binding [message #1759325 is a reply to message #1759310] Mon, 10 April 2017 06:41 Go to previous message
Charles Brouwers is currently offline Charles BrouwersFriend
Messages: 7
Registered: April 2017
Junior Member
Thanks a lot. This is easy to implement but I realize my ThingHandler.initialize() routine also calls code that will delay the main thread. I should probably fix but it raises a broader question that i will post separately.
Previous Topic:How to use Trigger-Channels?
Next Topic:Channels cannot be linked if dynamically created
Goto Forum:
  


Current Time: Thu Apr 18 12:04:59 GMT 2024

Powered by FUDForum. Page generated in 0.02989 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top