I am currently working on some communications code. Basically I am
sending HTTP Requests to my server at an interval of lets say around
100 ms.
The code is neatly placed in a Service and I am using threads in order
not to block my UI.
What I have right now is basically a cycle like this.
1. Schedule a handler to call a runnable
myHandler.postDelayed(myRunnable, 100); // 100 ms
2. Inside the runnable's run method I create a new Thread Object.
mThread = new Thread( this);
3. Then I run the thread.
mThread.start();
4. the run-method of the thread finishes at some point, either because
e.g. a timeout-exception occured or because it finished after it
received and correctly handled the server's response.
5. If everything's fine, I will then post the handler again. Back to 1
==>> loop!
I've seen a lot of Java-examples online that all contain a loop inside
the thread's run-method which is constantly paused using .sleep() and
then runs again in order to do the "work".
I am wondering wether my approach is ok in Android. While I am aware
that there must be some overhead when I create a new Thread object for
each cycle,
I wonder if it makes a huge difference, and if maybe the implications
of having a thread constanly sleeping in memory ( especially if it is
inactive for a longer time ) might not be bigger? I somehow feel it's
not an elegant solution to have a while loop in a thread with a sleep
simply to keep it alive....
Any comments grealty appreciated!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---