It is not a good idea to create a new thread every 100ms. As you noted there are overheads associated with thread creation. In a past project we noticed a significant memory impact from creating hundreds of thousands of new threads (albeit on windows system)
If you don't want to use the sleep paradigm (which is a perfectly acceptable approach IMO) you could use an executor from java.util.concurrent which will use a thread pool to service your threads instead of creating a new thread every time. Just use one of the static methods in java.util.concurrent.Executors to create the executor and then submit runnables to it. On Jan 5, 11:03 am, Noonien Soong <nooniensoong2...@gmail.com> wrote: > 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 android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---