{"componentChunkName":"component---src-templates-blog-post-js","path":"/multiple-asynctasks/2017-01-24-multiple-asynctasks/","result":{"data":{"site":{"siteMetadata":{"title":"Arjun","author":"Arjun"}},"markdownRemark":{"id":"095b695a-2de9-5c68-a672-023389ccf4ba","excerpt":"AsyncTask enables the proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without…","timeToRead":2,"html":"<p>AsyncTask enables the proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. This is the definition given for <code class=\"lang-text\">AsyncTask</code> in android documentation. Now let us see how this class works internally.</p>\n<p>AsyncTask uses <code class=\"lang-text\">Handler</code>, <code class=\"lang-text\">Thread</code>, <code class=\"lang-text\">Executor</code>, <code class=\"lang-text\">FutureTask</code>, <code class=\"lang-text\">Callable</code> to achieve its functionality, which is to perform the operation in background and post the status and result of the operation onto the main thread. Whenever a new AsyncTask object is created, a new <code class=\"lang-text\">Callable</code> and <code class=\"lang-text\">FutureTask</code> instance are created in the constructor.</p>\n<p>Every AsyncTask created will have a common <code class=\"lang-text\">THREAD_POOL_EXECUTOR</code> and <code class=\"lang-text\">SERIAL_EXECUTOR</code> at the process level to execute the tasks in a new thread.</p>\n<p><code class=\"lang-text\">SERIAL_EXECUTOR</code> - This is the default executor used in AsyncTask, which executed the AsyncTask in a sequential order, using a <code class=\"lang-text\">Queue</code> implementation. However, <code class=\"lang-text\">SERIAL_EXECUTOR</code> used <code class=\"lang-text\">THREAD_POOL_EXECUTOR</code> at the end to execute the task in a new thread.</p>\n<p><code class=\"lang-text\">THREAD_POOL_EXECUTOR</code> - This executor is used when the AsyncTask is executed using <code class=\"lang-text\">executeOnExecutor()</code> method. This executed uses Java’s <code class=\"lang-text\">ThreadPoolExecutor</code> to maintain a pool of threads and execute all the asynctasks using threads from that pool.</p>\n<p>So in order to execute multiple AsyncTask in parallel, we have to use <code class=\"lang-text\">THREAD_POOL_EXECUTOR</code> and execute the AsyncTasks.</p>\n<p>Below is the sample of running multiple AsyncTasks</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"lang-java\"><code class=\"lang-java\"><span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token class-name\">Request</span> req <span class=\"token operator\">:</span> allPendingRequest<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">new</span> <span class=\"token class-name\">BatchTask</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">executeOnExecutor</span><span class=\"token punctuation\">(</span><span class=\"token class-name\">AsyncTask</span><span class=\"token punctuation\">.</span>THREAD_POOL_EXECUTOR<span class=\"token punctuation\">,</span> req<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>What the above code does is, for all the parallel requests to be made, it creates a new AsyncTask and instead of executing it using <code class=\"lang-text\">SERIAL_EXECUTOR</code>(by using <code class=\"lang-text\">execute()</code> method), we pass in the <code class=\"lang-text\">AsyncTask.THREAD_POOL_EXECUTOR</code> to be used as executor for executing tasks in parallel.</p>\n<p>This <code class=\"lang-text\">AsyncTask.THREAD_POOL_EXECUTOR</code> will queue up all the task and execute it 5 tasks at a time and when any of running task is completed, the next on from the queue is executed. This is repeated until queue becomes empty. <code class=\"lang-text\">AsyncTask.THREAD_POOL_EXECUTOR</code> cannot queue more that 128 tasks at a time. So if we submit more than that, an exception will be thrown.</p>","frontmatter":{"title":"Multiple AsyncTasks","date":"January 24, 2017"}}},"pageContext":{"slug":"/multiple-asynctasks/2017-01-24-multiple-asynctasks/","previous":{"fields":{"slug":"/abstractvsinterface/2017-02-02-abstractvsinterface/"},"frontmatter":{"title":"Java abstract vs interface"}},"next":{"fields":{"slug":"/edittext_workaround/2016-08-30-edittext_workaround/"},"frontmatter":{"title":"Softkeyboard on EditText"}}}}}