1

I know an IntentService itself runs on a different thread.

Also that it execute the onHandleIntent() and stops when that method is done.

my question is: are there any consequences for creating my own custom Thread inside the intent service?

I know it can be done in a Service but I want to know if thats a wrong way of using IntentService

for a bit more information what I need to do is to send lots of HTTP requests.

What im about to do is save on a DB the request strings, and run intent service that execute them. That's why I use IntentService, the requests might take time and I want the service to shut down once the table containing the requests is empty.

I thought i might increase the speed of this service by adding my own threads to it as I will be running lets say, 5 threads each time.

EDIT: This is the code I thought to do, I guess it will clear things about what im trying to do and if its possible.

 protected void onHandleIntent(Intent intent) {
    helper = new DBHelper(getApplicationContext());
    File file;
    //checks if the DB requests exists
    while(helper.requestsExists()){
        ArrayList<String> requestArr = helper.getRequestsToExcute(5);
        if(!requestArr.isEmpty()){
            //execute them and delete the DB entry
            for(int i=0;i<requestArr.size();i++){
                file = new File(requestArr.get(i));
                new MyThread(file).start();// the DB entry is delete withing the thread
            }
        }
    }
}

so this service will run as long as it got any DB entries on my SQLite db, after it will finish executing all of them it will stop. is it ok or should i use Service for it?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
sharon gur
  • 343
  • 5
  • 22
  • Should be fine. IntentService itself handles async tasks so opening up a few extra threads with something like an ExecutorService would improve performance. Just make sure you manage the amount of threads that are opened. – the-ginger-geek May 25 '15 at 08:36
  • 1
    this makes no sens at all(misusage of intentservice): http://stackoverflow.com/questions/11700288/how-do-i-keep-the-thread-of-an-intentservice-alive – Selvin May 25 '15 at 08:42
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 25 '15 at 17:24

1 Answers1

0

As you have explained, the onHandleIntent is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic.

Why do you want to create a separate thread unless you want to handle multiple requests coming in and handle them in parallel ?

So, if you handle the action that you are suggesting in the onHandleIntent it will hold up other requests to the same IntentService, but it will not hold up anything else.

This answer might help you - start async task from onhandleintent

Community
  • 1
  • 1
Venki
  • 2,129
  • 6
  • 32
  • 54
  • edited the question to contain the piece of code I was thinking about doing, would love it if you review it, Thanks anyway – sharon gur May 25 '15 at 09:02