In my application , i need to run set of tasks sequentially. Each task unzip file and update data to database . But am getting fallowing exception while updat开发者_C百科ing data from different threads.
java.lang.IllegalStateException: database not open`
To prevent this , am using ThreadPoolExecutor to execute tasks sequentially so that i can synchronize database.So can any one tell me how to use ThreadPoolExecutor to accomplish my task. What parameters i have to pass.
Regards, Srinivas
How to make sure all statements are executed? I have tried executing another thread in asynctask but there is not guarantee of the AsyncTask will execute the thread which is inner to it and it will go in the postExecute method. You can check my example.
Use asynctask. It itself uses threadpoolexecutor and is quite handy to use. If you want to run tasks sequentially, be sure to call the next asynctask in the predecessing asynctask's onpostexecute.
To run same task with different set of data repeatedly and sequentially, the best option is to use IntentService. IntentService maintains a queue to hold tasks and executes them one by one using single worker thread.
Task producer sends requests to IntentService and IntentService adds task to queue and execute them sequentially in the background using single worker thread. To know how to use IntentService, see IntentService example.
精彩评论