Asynchronous task takes too much time to complete work

I need to send a command from the device, and progressdialog should display the progress before the command completes. For this, I use asynctask in my application. But it takes a long time to complete the team.

So, is there a way to set asynctask priority to maximum?

Any suggestion will be appreciated. thanks in advance

+4
source share
3 answers

Well you can try this:

 protected Void doInBackground() { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); /* do the work */ } 
+25
source

You can also define your own timeout if you do not want the user to wait for a response from the web service in a certain amount of time.

+2
source

Currently, Android uses a common thread chain for all threads with priority THREAD_PRIORITY_BACKGROUND or worse, and THREAD_PRIORITY_BACKGROUND is 10, and THREAD_PRIORITY_DEFAULT is 0 and THREAD_PRIORITY_FOREGROUND is -2.

If you switch to THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE (aka 9), your stream will be removed from the background group with a 10% limit, although it will not be important enough to interrupt UI threads too often.

  protected final YourResult doInBackground(YourInputs... yis) { Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE); ... } 

but keep in mind that the underlying implementation can reuse the same Thread object for different tasks, for the next AsyncTask or any other. It seems that Android just resets the priority after doInBackground () returns. .

0
source

All Articles