gdata.io.handleScriptLoaded({"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$georss":"http://www.georss.org/georss","xmlns$thr":"http://purl.org/syndication/thread/1.0","xmlns$blogger":"http://schemas.google.com/blogger/2008","id":{"$t":"tag:blogger.com,1999:blog-913600556879440043"},"updated":{"$t":"2024-01-01T15:38:08.302+05:30"},"category":[{"term":"Data Structures"},{"term":"Vedic Mathematics"},{"term":"My Vlogs"},{"term":"Website Designing"},{"term":"Guest Blogging"},{"term":"Youtube"},{"term":"PPL"},{"term":"Android"},{"term":"Android App Development"},{"term":"High Performance Computing"},{"term":"Socket Programming"},{"term":"Java"},{"term":"Cloud Computing"},{"term":"Unboxing \u0026 Review"},{"term":"Database"},{"term":"OpenMPI"},{"term":"OPENCL"},{"term":"CUDA"},{"term":"LEX \u0026 YACC"},{"term":"Vocabulary"},{"term":"Compiler"},{"term":"Blogging Tips"},{"term":"Networking"},{"term":"Linux"},{"term":"Nanded City Pune"},{"term":"Parallel Computing"},{"term":"SDL"},{"term":"Fedora"},{"term":"Udemy Courses"},{"term":"Dia Software"},{"term":"MPI"},{"term":"Multithreading"},{"term":"Computer Networks"},{"term":"Abbreviations in Computer Science"},{"term":"Salesforce"},{"term":"Lisp"},{"term":"YouTube Tips"},{"term":"MS Excel Formulas \u0026 Functions"},{"term":"C Plus Plus Programming"},{"term":"GATE"},{"term":"Mysql"},{"term":"Google Forms"},{"term":"Wine"},{"term":"Swing"},{"term":"Mathematics"},{"term":"SQL"},{"term":"Amazon Links Summary"},{"term":"Thread Pool"},{"term":"General"},{"term":"Amazon Affiliate Program"},{"term":"How To Write Blog"},{"term":"C Programming"},{"term":"Applet"},{"term":"Selenium Automation Testing"},{"term":"Skill Development Lab"},{"term":"OPENMP"},{"term":"Python"},{"term":"Ubuntu"}],"title":{"type":"text","$t":"Computer Revolution (www.comrevo.com)"},"subtitle":{"type":"html","$t":""},"link":[{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/-/Thread+Pool?alt\u003djson-in-script\u0026max-results\u003d6"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/-/Thread+Pool?alt\u003djson-in-script\u0026max-results\u003d6"},{"rel":"alternate","type":"text/html","href":"http://www.comrevo.com/search/label/Thread%20Pool"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"https://www.blogger.com/profile/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"35","height":"35","src":"//www.blogger.com/img/blogger_logo_round_35.png"}}],"generator":{"version":"7.00","uri":"https://www.blogger.com","$t":"Blogger"},"openSearch$totalResults":{"$t":"3"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"6"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-8914407605886362176"},"published":{"$t":"2017-08-02T09:38:00.001+05:30"},"updated":{"$t":"2017-08-31T09:55:04.184+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Thread Pool"}],"title":{"type":"text","$t":"Thread Pool in Java with example of matrix addition"},"content":{"type":"html","$t":"\u003cdiv dir\u003d\"ltr\" style\u003d\"text-align: left;\" trbidi\u003d\"on\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;In this post, we will see what is threadpool in Java along with an example of two matrices addition.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u003cb\u003eWhat is Threadpool?\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Threadpool is a concept in Java. It refers to the collection of threads i.e. a group of fixed size of threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eHow it works?\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;A thread is taken (i.e. pulled) from thread pool and task is allocated to it. Similarly, other threads are taken from thread pool and tasks are allocated to them. When task is completed, thread is returned to the thread pool. A returned thread in thread pool can be pulled back again and can be allocated a new task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Suppose there are three threads in a thread pool and five tasks.\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv class\u003d\"separator\" style\u003d\"clear: both; text-align: center;\"\u003e\n\u003ca href\u003d\"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s1600/threadpool.jpeg\" imageanchor\u003d\"1\" style\u003d\"margin-left: 1em; margin-right: 1em;\"\u003e\u003cimg border\u003d\"0\" data-original-height\u003d\"385\" data-original-width\u003d\"548\" src\u003d\"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s1600/threadpool.jpeg\" /\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(First Thread\u003dFirst Task) First thread will be allocated first task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Thread\u003d\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Task)\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond thread will be allocated second task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Thread\u003d\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Task)\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird thread will be allocated third task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eOnce the\u0026nbsp;\u003cu\u003efirst or second or third\u003c/u\u003e\u0026nbsp;thread will be free i.e. completed task; it will return to thread pool and it will be allocated fourth task.\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eAgain whoever thread gets free will return back to thread pool and will be allocated fifth task.\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eAdvantage of Thread Pool:\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Thread Pool reuses the threads. That's why, it reduces the time for creating new threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Java Thread Pool can be used with Servlet or JSP.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003cu\u003eGo through the following example:\u003c/u\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Here we are adding two matrices with 2 rows and 3 columns. We are creating a thread pool with 4 threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eThreadPoolMatrixAddition.java\u0026nbsp;\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;import java.util.concurrent.*; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eclass WorkerThread implements Runnable\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e{ \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; private int row;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; private int col;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; int x[][]\u003d{{1,2,3},{4,5,6}};\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; int y[][]\u003d{{1,2,3},{4,5,6}};\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; static int z[][]\u003dnew int[2][3]; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; public WorkerThread(int i, int j)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; this.row\u003di; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; this.col\u003dj;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;public void run()\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; z[row][col]\u003dx[row][col]+y[row][col];\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(\"z[\"+row+\"][\"+col+\"]\u003dx[\"+row+\"][\"+col+\"]+y[\"+row+\"][\"+col+\"] calculated by \"+Thread.currentThread().getName());\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e}\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epublic class ThreadPoolMatrixAddition\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e{ \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;public static void main(String[] args)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; ExecutorService executor \u003d Executors.newFixedThreadPool(4);//creating a pool of 4 threads \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; for (int i \u003d 0; i \u0026lt; 2; i++)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;for (int j \u003d 0; j \u0026lt; 3; j++)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Runnable worker \u003d new WorkerThread(i,j); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; executor.execute(worker);//calling execute method of ExecutorService \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; }\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;executor.shutdown();//shutdown() will not allow allocating new tasks to threads but will wait till the completion of all allocated tasks\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;while (!executor.isTerminated()) { }\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;System.out.println();\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;for (int i \u003d 0; i \u0026lt; 2; i++)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;for (int j \u003d 0; j \u0026lt; 3; j++)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(\"z[\"+i+\"][\"+j+\"]\u003d\"+WorkerThread.z[i][j]);\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; }\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;System.out.println(); \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;System.out.println(\"Finished all threads\"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;} \u0026nbsp;\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv dir\u003d\"ltr\" style\u003d\"orphans: auto; text-align: left; text-indent: 0px; widows: 1;\" trbidi\u003d\"on\"\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman';\"\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eOutput:\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/prog$ javac ThreadPoolMatrixAddition.java\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/prog$ java ThreadPoolMatrixAddition\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0][0]\u003dx[0][0]+y[0][0] calculated by pool-1-thread-1\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0][2]\u003dx[0][2]+y[0][2] calculated by pool-1-thread-3\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1][0]\u003dx[1][0]+y[1][0] calculated by pool-1-thread-4\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0][1]\u003dx[0][1]+y[0][1] calculated by pool-1-thread-2\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1][2]\u003dx[1][2]+y[1][2] calculated by pool-1-thread-3\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1][1]\u003dx[1][1]+y[1][1] calculated by pool-1-thread-1\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0][0]\u003d2\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0][1]\u003d4\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0][2]\u003d6\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1][0]\u003d8\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1][1]\u003d10\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1][2]\u003d12\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eFinished all threads\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv\u003e\n\u003cbr /\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003eCheck other posts on Multi-Threading in this link\u0026nbsp;\u003ca href\u003d\"http://www.comrevo.com/2016/09/multi-threading.html\" target\u003d\"_blank\"\u003ehttp://www.comrevo.com/2016/09/multi-threading.html\u003c/a\u003e\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https://www.comrevo.com/feeds/8914407605886362176/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2017/08/java-thread-pool-with-example-of-matrix-addition.html#comment-form","title":"6 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/8914407605886362176"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/8914407605886362176"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2017/08/java-thread-pool-with-example-of-matrix-addition.html","title":"Thread Pool in Java with example of matrix addition"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"https://www.blogger.com/profile/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"35","height":"35","src":"//www.blogger.com/img/blogger_logo_round_35.png"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s72-c/threadpool.jpeg","height":"72","width":"72"},"thr$total":{"$t":"6"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-779478028340449851"},"published":{"$t":"2017-08-01T22:02:00.000+05:30"},"updated":{"$t":"2017-08-31T09:54:03.232+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Thread Pool"}],"title":{"type":"text","$t":"Thread Pool in Java with example of array addition"},"content":{"type":"html","$t":"\u003cdiv dir\u003d\"ltr\" style\u003d\"text-align: left;\" trbidi\u003d\"on\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;In this post, we will see what is threadpool in Java along with an example of two arrays addition.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u003cb\u003eWhat is Threadpool?\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Threadpool is a concept in Java. It refers to the collection of threads i.e. a group of fixed size of threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eHow it works?\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;A thread is taken (i.e. pulled) from thread pool and task is allocated to it. Similarly, other threads are taken from thread pool and tasks are allocated to them. When task is completed, thread is returned to the thread pool. A returned thread in thread pool can be pulled back again and can be allocated a new task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Suppose there are three threads in a thread pool and five tasks.\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv class\u003d\"separator\" style\u003d\"clear: both; text-align: center;\"\u003e\n\u003ca href\u003d\"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s1600/threadpool.jpeg\" imageanchor\u003d\"1\" style\u003d\"margin-left: 1em; margin-right: 1em;\"\u003e\u003cimg border\u003d\"0\" data-original-height\u003d\"385\" data-original-width\u003d\"548\" src\u003d\"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s1600/threadpool.jpeg\" /\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(First Thread\u003dFirst Task) First thread will be allocated first task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Thread\u003d\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Task)\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond thread will be allocated second task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Thread\u003d\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Task)\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird thread will be allocated third task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eOnce the\u0026nbsp;\u003cu\u003efirst or second or third\u003c/u\u003e\u0026nbsp;thread will be free i.e. completed task; it will return to thread pool and it will be allocated fourth task.\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eAgain whoever thread gets free will return back to thread pool and will be allocated fifth task.\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eAdvantage of Thread Pool:\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Thread Pool reuses the threads. That's why, it reduces the time for creating new threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Java Thread Pool can be used with Servlet or JSP.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003cu\u003eGo through the following example:\u003c/u\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Here we are adding two arrays with 10 elements each. We are creating a thread pool with 5 threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eThreadPoolExample.java\u0026nbsp;\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eimport java.util.concurrent.*; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eclass WorkerThread implements Runnable\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e{ \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; private int num;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; int x[]\u003d{1,2,3,4,5,6,7,8,9,10};\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; int y[]\u003d{1,2,3,4,5,6,7,8,9,10};\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; static int z[]\u003dnew int[10]; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; public WorkerThread(int j)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; this.num\u003dj; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;public void run()\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; z[num]\u003dx[num]+y[num];\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(\"z[\"+num+\"]\u003dx[\"+num+\"]+y[\"\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e+num+\"]\"+\" calculated by \"+Thread.currentThread().getName());\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e}\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epublic class ThreadPoolExample\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e{ \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;public static void main(String[] args)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; ExecutorService executor \u003d Executors.newFixedThreadPool(5);//creating a pool of 5 threads \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; for (int i \u003d 0; i \u0026lt; 10; i++)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Runnable worker \u003d new WorkerThread(i); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; executor.execute(worker);//calling execute method of ExecutorService \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; }\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;executor.shutdown();//shutdown() will not allow allocating new tasks to threads but will wait till the completion of all allocated tasks\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;while (!executor.isTerminated()) { }\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;for (int i \u003d 0; i \u0026lt; 10; i++)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(\"z[\"+i+\"]\u003d\"+WorkerThread.z[i]);\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;System.out.println(\"Finished all threads\"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;} \u0026nbsp;\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv dir\u003d\"ltr\" style\u003d\"orphans: auto; text-align: left; text-indent: 0px; widows: 1;\" trbidi\u003d\"on\"\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman';\"\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eOutput:\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/prog$ javac ThreadPoolExample.java\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/prog$ java ThreadPoolExample\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1]\u003dx[1]+y[1] calculated by pool-1-thread-2\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[3]\u003dx[3]+y[3] calculated by pool-1-thread-4\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0]\u003dx[0]+y[0] calculated by pool-1-thread-1\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[2]\u003dx[2]+y[2] calculated by pool-1-thread-3\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[8]\u003dx[8]+y[8] calculated by pool-1-thread-3\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[7]\u003dx[7]+y[7] calculated by pool-1-thread-4\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[6]\u003dx[6]+y[6] calculated by pool-1-thread-1\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[5]\u003dx[5]+y[5] calculated by pool-1-thread-2\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[9]\u003dx[9]+y[9] calculated by pool-1-thread-3\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[4]\u003dx[4]+y[4] calculated by pool-1-thread-5\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[0]\u003d2\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[1]\u003d4\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[2]\u003d6\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[3]\u003d8\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[4]\u003d10\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[5]\u003d12\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[6]\u003d14\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[7]\u003d16\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[8]\u003d18\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ez[9]\u003d20\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eFinished all threads\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003eCheck other posts on Multi-Threading in this link\u0026nbsp;\u003ca href\u003d\"http://www.comrevo.com/2016/09/multi-threading.html\" target\u003d\"_blank\"\u003ehttp://www.comrevo.com/2016/09/multi-threading.html\u003c/a\u003e\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https://www.comrevo.com/feeds/779478028340449851/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2017/08/java-thread-pool-with-example-of-array-addition.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/779478028340449851"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/779478028340449851"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2017/08/java-thread-pool-with-example-of-array-addition.html","title":"Thread Pool in Java with example of array addition"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"https://www.blogger.com/profile/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"35","height":"35","src":"//www.blogger.com/img/blogger_logo_round_35.png"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s72-c/threadpool.jpeg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-3940970730343430334"},"published":{"$t":"2017-07-25T21:50:00.002+05:30"},"updated":{"$t":"2020-09-08T23:37:05.143+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Thread Pool"}],"title":{"type":"text","$t":"Thread Pool in Java with Example"},"content":{"type":"html","$t":"\u003cdiv dir\u003d\"ltr\" style\u003d\"text-align: left;\" trbidi\u003d\"on\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;In this post, we will see what is threadpool in Java along with an example,\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThread Pool in Java Multithreading with Example | Threadpool Java | Executorservice in Java | thread pool in java,thread pool executor in java,thread pool,threadpoolexecutor in java,threadpool java,thread pool,threadpoolexecutor,thread pool in java multithreading,thread pool in java example,java thread pool,executor thread pool in java\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eWatch this video to know Threadpool in Java:\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003ciframe allow\u003d\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003d\"\" frameborder\u003d\"0\" height\u003d\"360\" src\u003d\"https://www.youtube.com/embed/eeO2C6dfALc\" width\u003d\"640\"\u003e\u003c/iframe\u003e\n\n\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eWatch on YouTube:\u0026nbsp;\u003ca href\u003d\"https://www.youtube.com/watch?v\u003deeO2C6dfALc\" target\u003d\"_blank\"\u003ehttps://www.youtube.com/watch?v\u003deeO2C6dfALc\u003c/a\u003e\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u003cb\u003eWhat is Threadpool?\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Threadpool is a concept in Java. It refers to the collection of threads i.e. a group of fixed size of threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eHow it works?\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;A thread is taken (i.e. pulled) from thread pool and task is allocated to it. Similarly, other threads are taken from thread pool and tasks are allocated to them. When task is completed, thread is returned to the thread pool. A returned thread in thread pool can be pulled back again and can be allocated a new task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Suppose there are three threads in a thread pool and five tasks.\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv class\u003d\"separator\" style\u003d\"clear: both; text-align: center;\"\u003e\n\u003ca href\u003d\"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s1600/threadpool.jpeg\" imageanchor\u003d\"1\" style\u003d\"margin-left: 1em; margin-right: 1em;\"\u003e\u003cimg border\u003d\"0\" data-original-height\u003d\"385\" data-original-width\u003d\"548\" src\u003d\"https://4.bp.blogspot.com/-brL_Yz57yxQ/WXxkDwAGE-I/AAAAAAAADl8/4i9j0Q-OlHwi7sT7vYI8E-ZSyHnp58O6ACLcBGAs/s1600/threadpool.jpeg\" /\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(First Thread\u003dFirst Task) First thread will be allocated first task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Thread\u003d\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Task)\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eSecond thread will be allocated second task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e(\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Thread\u003d\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;Task)\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThird thread will be allocated third task.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eOnce the \u003cu\u003efirst or second or third\u003c/u\u003e thread will be free i.e. completed task; it will return to thread pool and it will be allocated fourth task.\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eAgain whoever thread gets free will return back to thread pool and will be allocated fifth task.\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eAdvantage of Thread Pool:\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Thread Pool reuses the threads. That's why, it reduces the time for creating new threads.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Java Thread Pool can be used with Servlet or JSP.\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003cu\u003eGo through the following example:\u003c/u\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eThreadPoolExample.java\u0026nbsp;\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003e\u003cbr /\u003e\u003c/b\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv dir\u003d\"ltr\" style\u003d\"orphans: auto; text-align: left; text-indent: 0px; widows: 1;\" trbidi\u003d\"on\"\u003e\n\u003cdiv style\u003d\"background-color: lightgreen;\"\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;import java.util.concurrent.*; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eclass WorkerThread implements Runnable\u0026nbsp;\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e{ \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; private int num; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; public WorkerThread(int j)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; this.num\u003dj; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;public void run()\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(Thread.currentThread().getName()+\" (Start) num \u003d \"+num); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; try {Thread.sleep(2000);} catch(Exception e) { } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(Thread.currentThread().getName()+\" (End)\"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e}\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epublic class ThreadPoolExample\u0026nbsp;\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e{ \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;public static void main(String[] args)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; ExecutorService executor \u003d Executors.newFixedThreadPool(5);//creating a pool of 5 threads \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; for (int i \u003d 0; i \u0026lt; 10; i++)\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; { \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Runnable worker \u003d new WorkerThread(i); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; executor.execute(worker);//calling execute method of ExecutorService \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; executor.shutdown();//shutdown() will not allow allocating new tasks to threads but will wait till the completion of all allocated tasks\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; while (!executor.isTerminated()) { }\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; System.out.println(\"Finished all threads\"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; } \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;} \u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman';\"\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman'; margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eOutput:\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr class\u003d\"Apple-interchange-newline\" /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/JDBC$ javac ThreadPoolExample.java\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/JDBC$ java ThreadPoolExample\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-1 (Start) num \u003d 0\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-5 (Start) num \u003d 4\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-3 (Start) num \u003d 2\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-4 (Start) num \u003d 3\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-2 (Start) num \u003d 1\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-1 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-3 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-1 (Start) num \u003d 5\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-4 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-5 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-5 (Start) num \u003d 8\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-4 (Start) num \u003d 7\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-3 (Start) num \u003d 6\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-2 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-2 (Start) num \u003d 9\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-1 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-4 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-3 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-2 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003epool-1-thread-5 (End)\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eFinished all threads \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-family: \u0026quot;times new roman\u0026quot;; font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-family: inherit; font-size: large;\"\u003eCheck\u0026nbsp;\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cspan style\u003d\"background-color: white;\"\u003e\u003cspan style\u003d\"font-family: inherit; font-size: large;\"\u003eThread Pool in Java with example of array addition i this link\u0026nbsp;\u003ca href\u003d\"http://www.comrevo.com/2017/08/java-thread-pool-with-example-of-array-addition.html\" target\u003d\"_blank\"\u003ehttp://www.comrevo.com/2017/08/java-thread-pool-with-example-of-array-addition.html\u003c/a\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-family: inherit; font-size: large;\"\u003eCheck Thread Pool in Java with example of matrix addition in this link\u0026nbsp;\u003ca href\u003d\"http://www.comrevo.com/2017/08/java-thread-pool-with-example-of-matrix-addition.html\" target\u003d\"_blank\"\u003ehttp://www.comrevo.com/2017/08/java-thread-pool-with-example-of-matrix-addition.html\u003c/a\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-family: inherit; font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003eCheck other posts on Multi-Threading in this link\u0026nbsp;\u003ca href\u003d\"http://www.comrevo.com/2016/09/multi-threading.html\" target\u003d\"_blank\"\u003ehttp://www.comrevo.com/2016/09/multi-threading.html\u003c/a\u003e\u003c/span\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman';\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https://www.comrevo.com/feeds/3940970730343430334/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2017/07/thread-pool-in-java-with-example.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/3940970730343430334"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/3940970730343430334"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2017/07/thread-pool-in-java-with-example.html","title":"Thread Pool in Java with Example"}],"author":[{"name":{"$t":"Parag Jambhulkar"},"uri":{"$t":"https://www.blogger.com/profile/13991750622483538113"},"email":{"$t":"noreply@blogger.com"},"gd$image":{"rel":"http://schemas.google.com/g/2005#thumbnail","width":"35","height":"35","src":"//www.blogger.com/img/blogger_logo_round_35.png"}}],"media$thumbnail":{"xmlns$media":"http://search.yahoo.com/mrss/","url":"https://i.ytimg.com/vi/eeO2C6dfALc/default.jpg","height":"72","width":"72"},"thr$total":{"$t":"0"}}]}});