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/-/Multithreading?alt\u003djson-in-script\u0026max-results\u003d6"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/-/Multithreading?alt\u003djson-in-script\u0026max-results\u003d6"},{"rel":"alternate","type":"text/html","href":"http://www.comrevo.com/search/label/Multithreading"},{"rel":"hub","href":"http://pubsubhubbub.appspot.com/"},{"rel":"next","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/-/Multithreading/-/Multithreading?alt\u003djson-in-script\u0026start-index\u003d7\u0026max-results\u003d6"}],"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":"11"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"6"},"entry":[{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-9181867006948546724"},"published":{"$t":"2020-09-01T16:44:00.000+05:30"},"updated":{"$t":"2020-09-05T12:32:34.868+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Skill Development Lab"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"}],"title":{"type":"text","$t":"Thread Life Cycle in Java | Different Phases in the Life Cycle of Thread | Thread Life Cycle Diagram"},"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; \u0026nbsp; In this post, we will see\u0026nbsp;\u003c/span\u003e\u003cspan style\u003d\"font-size: large;\"\u003eThread Life Cycle in Java | Different Phases in the Life Cycle of Thread | Thread Life Cycle Diagram | thread life cycle in java,thread life cycle,thread life cycle in java with example,thread life cycle diagram,thread life cycle methods in java,life cycle of thread in java,life cycle of thread.\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 different phases in the life cycle of thread:\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/7EptP0bli2w\" width\u003d\"640\"\u003e\u003c/iframe\u003e\n\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003e\u003cbr /\u003e\u003c/b\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eWatch on YouTube:\u0026nbsp;\u003ca href\u003d\"https://www.youtube.com/watch?v\u003d7EptP0bli2w\" target\u003d\"_blank\"\u003ehttps://www.youtube.com/watch?v\u003d7EptP0bli2w\u003c/a\u003e\u003c/b\u003e\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\u003c/span\u003e\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eCheck following Diagram:\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003cdiv class\u003d\"separator\" style\u003d\"clear: both; text-align: center;\"\u003e\n\u003ca href\u003d\"https://1.bp.blogspot.com/-o6I9l_Fxwh4/X04s9QuNb_I/AAAAAAAAEcE/1186MLF5wYMAS5w78f3JdkoTyN1IGk2KQCNcBGAsYHQ/s1600/thread%2Blife%2Bcycle.jpeg\" imageanchor\u003d\"1\" style\u003d\"margin-left: 1em; margin-right: 1em;\"\u003e\u003cimg border\u003d\"0\" data-original-height\u003d\"434\" data-original-width\u003d\"653\" src\u003d\"https://1.bp.blogspot.com/-o6I9l_Fxwh4/X04s9QuNb_I/AAAAAAAAEcE/1186MLF5wYMAS5w78f3JdkoTyN1IGk2KQCNcBGAsYHQ/s1600/thread%2Blife%2Bcycle.jpeg\" /\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class\u003d\"separator\" style\u003d\"clear: both; text-align: center;\"\u003e\n\u003c/div\u003e\n\u003cdiv class\u003d\"separator\" style\u003d\"clear: both; text-align: center;\"\u003e\n\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https://www.comrevo.com/feeds/9181867006948546724/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2020/09/thread-life-cycle-in-java-different-phases-in-life-cycle-of-thread.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/9181867006948546724"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/9181867006948546724"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2020/09/thread-life-cycle-in-java-different-phases-in-life-cycle-of-thread.html","title":"Thread Life Cycle in Java | Different Phases in the Life Cycle of Thread | Thread Life Cycle Diagram"}],"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/7EptP0bli2w/default.jpg","height":"72","width":"72"},"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-8510070930904202448"},"published":{"$t":"2019-08-14T12:09:00.000+05:30"},"updated":{"$t":"2019-08-14T12:12:31.439+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"}],"title":{"type":"text","$t":"wait notify and notifyAll example in Java | Multithreading synchronization in Java using wait, notify, notifyAll"},"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; \u0026nbsp; In this post, we will see \"wait notify and notifyall example in Java | Multithreading synchronization in Java using wait, notify, notifyall\" \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; wait(), notify(), notifyAll() methods are used in Java for multi-threading synchronization.\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cb\u003e\u003cspan style\u003d\"font-size: large;\"\u003ewait() method:\u003c/span\u003e\u003c/b\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp; When any thread calls wait() method, it goes to waiting mode and allow other waiting threads to execute. Multiple threads can call wait() method and go to the waiting mode.\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cb\u003e\u003cspan style\u003d\"font-size: large;\"\u003enotify() method:\u003c/span\u003e\u003c/b\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; When any thread calls notify method, it frees the other thread who has called wait() first and allow it to execute in critical section.\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cb\u003e\u003cspan style\u003d\"font-size: large;\"\u003enotifyAll() method:\u003c/span\u003e\u003c/b\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp; notifyAll() method frees all the threads who has called wait() method. \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\u003cdiv style\u003d\"font-family: 'Times New Roman';\"\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cdiv dir\u003d\"ltr\" style\u003d\"font-family: 'Times New Roman';\" trbidi\u003d\"on\"\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman';\"\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\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\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-family: inherit;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Go through the following program. Here, we are creating two threads. One thread is withdrawing money while other thread is depositing money. If balance is less, then thread who is withdrawing money will call wait() method and goes to waiting mode. It allows other thread to deposit first. Once other thread deposits money, it calls notify() method which allows first thread to withdraw money.\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\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cdiv style\u003d\"font-family: 'Times New Roman';\"\u003e\n\u003cb style\u003d\"font-size: large;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003eProgram (waitnotify.java)\u003c/span\u003e\u003c/b\u003e\u003c/div\u003e\n\u003ctable border\u003d\"1\" style\u003d\"font-family: 'Times New Roman';\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eclass Customer\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; int amount\u003d10000; \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; synchronized void withdraw(int amount)\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;System.out.println(\"going to withdraw...\"); \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; \u0026nbsp;if(this.amount\u0026lt;amount)\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(\"Less balance; waiting for deposit...\"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;try{wait();}\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;catch(Exception e){} \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;this.amount\u003dthis.amount-amount; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;System.out.println(\"withdraw completed...\"); \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\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; synchronized void deposit(int amount)\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;System.out.println(\"going to deposit...\"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;this.amount\u003dthis.amount+amount; \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;System.out.println(\"deposit completed... \"); \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp;notify(); \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\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eclass waitnotify\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; public static void main(String args[])\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;Customer c\u003dnew Customer(); \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;new Thread()\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; public void run()\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;{c.withdraw(15000);}\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; }.start(); \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;new Thread()\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; public void run()\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;{c.deposit(10000);} \u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; }.start(); \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;}\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;\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cdiv style\u003d\"font-family: 'Times New Roman';\"\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003cdiv style\u003d\"font-family: 'Times New Roman';\"\u003e\n\u003cb style\u003d\"font-family: 'times new roman';\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003eOutput:\u003c/span\u003e\u003c/b\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\n\u003cbr /\u003e\n\u003cdiv dir\u003d\"ltr\" style\u003d\"font-family: 'Times New Roman';\" trbidi\u003d\"on\"\u003e\n\u003cdiv style\u003d\"font-family: 'times new roman';\"\u003e\n\u003cdiv style\u003d\"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;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/progbythread$ javac waitnotify.java\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/progbythread$ java waitnotify\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003egoing to withdraw...\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eLess balance; waiting for deposit...\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003egoing to deposit...\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003edeposit completed...\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003ewithdraw completed...\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\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\u003cbr /\u003e\u003c/span\u003e\n\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv style\u003d\"font-family: 'Times New Roman';\"\u003e\n\u003cdiv style\u003d\"margin: 0px;\"\u003e\n\u003cspan style\u003d\"background-color: #fefdfa; font-family: \u0026quot;georgia\u0026quot; , \u0026quot;utopia\u0026quot; , \u0026quot;palatino linotype\u0026quot; , \u0026quot;palatino\u0026quot; , serif; font-size: large;\"\u003e\u0026nbsp; \u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\u003c/div\u003e\n\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/8510070930904202448/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2019/08/wait-notify-and-notifyAll-example-in-Java-Multithreading-synchronization-in-Java-using-wait-notify-notifyAll.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/8510070930904202448"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/8510070930904202448"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2019/08/wait-notify-and-notifyAll-example-in-Java-Multithreading-synchronization-in-Java-using-wait-notify-notifyAll.html","title":"wait notify and notifyAll example in Java | Multithreading synchronization in Java using wait, notify, notifyAll"}],"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"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-4139270934488946011"},"published":{"$t":"2019-08-07T12:15:00.000+05:30"},"updated":{"$t":"2019-08-07T12:15:07.727+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"}],"title":{"type":"text","$t":"Multi-threading synchronization in Java using Reentrant Lock"},"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;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In this post, we will see Multi-threading synchronization in Java using Reentrant Lock.\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In previous post, we have seen\u0026nbsp;Multi-threading Synchronization using \u003cu\u003esynchronized method\u003c/u\u003e. Check this link:\u0026nbsp;\u003ca href\u003d\"https://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-example-programs.html\" target\u003d\"_blank\"\u003ehttps://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-example-programs.html\u003c/a\u003e .\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp;\u0026nbsp; In this post, we will see how to achieve synchronization using lock. \u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eHow ReentrantLock is different from synchronized method/block?\u003c/b\u003e\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Reentrant Lock has extra advantages over synchronized method/block.\u0026nbsp;\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;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; In synchronized method/block, any thread among the waiting threads will be allowed to execute while if we use ReentrantLock, then the thread with more waiting time will be allowed to execute first.\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;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; ReentrantLock provides some methods, by using which we can terminate the thread which is waiting for longer time.\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;\"\u003eHow to use ReentrantLock?\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;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Lock is an interface provided by package java.util.concurrent.locks. Its implementation is provided by class ReentrantLock. Lock interface provides method lock() to allow calling thread to enter in critical section while other threads wait. unlock() method frees the locking thread and allow other threads to enter in critical section.\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;\"\u003eNote: critical section is a section or part of program where common resources (variables) get accessed.\u003c/span\u003e\u003cbr /\u003e\n\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;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Check following program without synchronization:\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: medium;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eProgram (SynchronizationLock.java)\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003eclass T1 \u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; int p\u003d0;\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; public void add()\u003cbr /\u003e\u0026nbsp;\u0026nbsp; {\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; p\u003dp+1;\u003cbr /\u003e\u0026nbsp;\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass T extends Thread\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; T1 t1\u003dnew T1();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; public T(T1 t2)\u003cbr /\u003e\u0026nbsp; {\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp; this.t1\u003dt2;\u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; public void run() \u003cbr /\u003e\u0026nbsp; { \u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp; t1.add();\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass SynchronizationLock\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; public static void main(String args[]) throws Exception \u003cbr /\u003e\u0026nbsp; {\u003cbr /\u003e\u0026nbsp; int i;\u003cbr /\u003e\u0026nbsp; T1 t1\u003dnew T1();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; T q[]\u003dnew T[300];\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i]\u003dnew T(t1);\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].start();\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].join();\u0026nbsp; \u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; System.out.println(\"Value of p:\"+t1.p);\u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e} \u003c/span\u003e\u003cbr /\u003e\n\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\u003cbr /\u003e\n\u003cb style\u003d\"font-family: 'times new roman'; font-size: x-large;\"\u003eOutput:\u0026nbsp;\u003c/b\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ javac SynchronizationLock.java \u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:298\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:297\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:296\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:298 \u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Check above program and its output. In this\n program, we are creating 300 threads. All these 300 threads are trying \nto increment value of variable p which was initialized to 0 in the \nbeginning. \u0026nbsp;Now check the output, sometimes we are getting value of p as\n 298, sometimes 300, sometimes 299, and sometimes 297. The reason behind\n this is few threads are incrementing the value of p simultaneously.\u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Here, p\u003dp+1 is a critical section. We need to synchronize 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\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Now, let us use \u003cb\u003eLock \u003c/b\u003einterface\u003cb\u003e \u0026amp; ReenttrantLock\u003c/b\u003e class. Check following program:\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: medium;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eProgram (SynchronizationLock.java)\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style\u003d\"font-size: large;\"\u003eimport java.util.concurrent.locks.*;\u003cbr /\u003e\u003cbr /\u003eclass T1 \u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; int p\u003d0;\u003cbr /\u003e\u0026nbsp; Lock queueLock \u003d new ReentrantLock();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; public void add()\u003cbr /\u003e\u0026nbsp;\u0026nbsp; {\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; queueLock.lock();\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; p\u003dp+1;\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; queueLock.unlock();\u003cbr /\u003e\u0026nbsp;\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass T extends Thread\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; T1 t1\u003dnew T1();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; public T(T1 t2)\u003cbr /\u003e\u0026nbsp; {\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp; this.t1\u003dt2;\u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; public void run() \u003cbr /\u003e\u0026nbsp; { \u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp; t1.add();\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass SynchronizationLock\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; public static void main(String args[]) throws Exception \u003cbr /\u003e\u0026nbsp; {\u003cbr /\u003e\u0026nbsp; int i;\u003cbr /\u003e\u0026nbsp; T1 t1\u003dnew T1();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; T q[]\u003dnew T[300];\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i]\u003dnew T(t1);\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].start();\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].join();\u0026nbsp; \u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; System.out.println(\"Value of p:\"+t1.p);\u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e} \u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\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\u003cbr /\u003e\n\u003cb style\u003d\"font-family: 'times new roman'; font-size: x-large;\"\u003eOutput:\u0026nbsp;\u003c/b\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;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ javac SynchronizationLock.java parag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationLock\u003cbr /\u003eValue of p:300\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;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Check above program. p\u003dp+1 was the critical \nsection where common resource (variable) p was used. We have locked that statement for a thread which calls lock() method. \u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Here, each time, we are getting value of p as 300 \nas only one thread is allowed execute statement p\u003dp+1 while all \nother threads were waiting.\u003c/span\u003e\u003c/div\u003e\n\u003c/div\u003e\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https://www.comrevo.com/feeds/4139270934488946011/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2019/08/Multi-threading-synchronization-in-Java-using-Reentrant-Lock.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/4139270934488946011"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/4139270934488946011"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2019/08/Multi-threading-synchronization-in-Java-using-Reentrant-Lock.html","title":"Multi-threading synchronization in Java using Reentrant Lock"}],"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"}}],"thr$total":{"$t":"0"}},{"id":{"$t":"tag:blogger.com,1999:blog-913600556879440043.post-4046296353839211293"},"published":{"$t":"2019-08-07T11:32:00.001+05:30"},"updated":{"$t":"2019-08-07T11:38:21.402+05:30"},"category":[{"scheme":"http://www.blogger.com/atom/ns#","term":"Java"},{"scheme":"http://www.blogger.com/atom/ns#","term":"Multithreading"}],"title":{"type":"text","$t":"Multi-threading Synchronization in Java with Synchronized Static Method"},"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;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In this post, we will see Multi-threading Synchronization in Java with Synchronized Static Method.\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003ca name\u003d'more'\u003e\u003c/a\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In previous post, we have seen\u0026nbsp;Multi-threading Synchronization using \u003cu\u003esynchronized method\u003c/u\u003e. Check this link:\u0026nbsp;\u003ca href\u003d\"https://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-example-programs.html\" target\u003d\"_blank\"\u003ehttps://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-example-programs.html\u003c/a\u003e .\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp; Also, we have seen how to use static block in this link:\u0026nbsp;\u003ca href\u003d\"https://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-synchronized-block.html\" target\u003d\"_blank\"\u003ehttps://www.comrevo.com/2018/09/multi-threading-synchronization-in-java-with-synchronized-block.html\u003c/a\u003e .\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u0026nbsp; In this post, we will use synchronization for static method. \u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eHow static method is different from non-static method?\u003c/b\u003e\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; In order to declare any method static, we have to use keyword \u003cb\u003estatic\u003c/b\u003e in method definition. In static method, we can only use \u003cb\u003estatic variables\u003c/b\u003e. Static methods can be called by \u003cb\u003eclass name\u003c/b\u003e directly. No need to create object.\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\u003e\n\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;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; Check following program without synchronization:\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: medium;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eProgram (SynchronizationStatic.java)\u003c/b\u003e\u003c/span\u003e\u003c/span\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style\u003d\"font-size: large;\"\u003eclass T1 \u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; static int p\u003d0;\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; public static void add()\u003cbr /\u003e\u0026nbsp;\u0026nbsp; {\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; p\u003dp+1;\u003cbr /\u003e\u0026nbsp;\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass T extends Thread\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; public void run() \u003cbr /\u003e\u0026nbsp; { \u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp; T1.add();\u0026nbsp;\u0026nbsp;\u0026nbsp; \u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass SynchronizationStatic\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; public static void main(String args[]) throws Exception \u003cbr /\u003e\u0026nbsp; {\u003cbr /\u003e\u0026nbsp; int i;\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; T q[]\u003dnew T[300];\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i]\u003dnew T();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].start();\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].join();\u0026nbsp; \u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; System.out.println(\"Value of p:\"+T1.p);\u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e}\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\u003cbr /\u003e\n\u003cb style\u003d\"font-family: 'times new roman'; font-size: x-large;\"\u003eOutput:\u0026nbsp;\u003c/b\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;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ javac SynchronizationStatic.java \u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:294\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:299\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:297\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:299\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:299\u003c/span\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003c/tbody\u003e\u003c/table\u003e\n\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; Check above program and its output. In this\n program, we are creating 300 threads. All these 300 threads are trying \nto increment value of variable p which was initialized to 0 in the \nbeginning. \u0026nbsp;Now check the output, sometimes we are getting value of p as\n 298, sometimes 300, sometimes 299, and sometimes 297. The reason behind\n this is few threads are incrementing the value of p simultaneously.\u0026nbsp;\u003c/span\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cdiv\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Here, p\u003dp+1 is a critical section. We need to synchronize 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\u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Now, let us use \u003cb\u003esynchronized\u003c/b\u003e keyword. Check following program:\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: medium;\"\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u003cb\u003eProgram (SynchronizationStatic.java)\u003c/b\u003e\u003c/span\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;\"\u003eclass T1 \u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; static int p\u003d0;\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; \u003cb\u003esynchronized\u003c/b\u003e public static void add()\u003cbr /\u003e\u0026nbsp;\u0026nbsp; {\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; p\u003dp+1;\u003cbr /\u003e\u0026nbsp;\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass T extends Thread\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; public void run() \u003cbr /\u003e\u0026nbsp; { \u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp; T1.add();\u0026nbsp;\u0026nbsp; \u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e}\u003cbr /\u003e\u003cbr /\u003eclass SynchronizationStatic\u003cbr /\u003e{\u003cbr /\u003e\u0026nbsp; public static void main(String args[]) throws Exception \u003cbr /\u003e\u0026nbsp; {\u003cbr /\u003e\u0026nbsp; int i;\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; T q[]\u003dnew T[300];\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i]\u003dnew T();\u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].start();\u003cbr /\u003e\u0026nbsp; \u003cbr /\u003e\u0026nbsp; for(i\u003d0;i\u0026lt;300;i++)\u003cbr /\u003e\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; q[i].join(); \u003cbr /\u003e\u003cbr /\u003e\u0026nbsp; System.out.println(\"Value of p:\"+T1.p);\u003cbr /\u003e\u0026nbsp; }\u003cbr /\u003e}\u003c/span\u003e\u003cbr /\u003e\n\u003cbr /\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\u003cbr /\u003e\n\u003cb style\u003d\"font-family: 'times new roman'; font-size: x-large;\"\u003eOutput:\u0026nbsp;\u003c/b\u003e\u003cbr /\u003e\n\u003ctable border\u003d\"1\"\u003e\u003ctbody\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ javac SynchronizationStatic.java\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:300\u003cbr /\u003eparag@parag-Inspiron-N4010:~/Desktop/programs/thread$ java SynchronizationStatic\u003cbr /\u003eValue of p:300\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;\u0026nbsp;\u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Check above program. p\u003dp+1 was the critical \nsection where common resource (variable) p was used. We mentioned that \nstatement in static method add(). \u003c/span\u003e\u003cbr /\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u0026nbsp;\n \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;Here, each time, we are getting value of p as 300 \nas only one thread is allowed in synchronized static method at once while all \nother threads were waiting.\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; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp; \u0026nbsp;\u003cb\u003eHow to use 'Multi-threading synchronization using lock', check in next post.\u003c/b\u003e\u003c/span\u003e\u003c/div\u003e\n\u003cspan style\u003d\"font-size: large;\"\u003e\u003cbr /\u003e\u003c/span\u003e\u003c/div\u003e\n"},"link":[{"rel":"replies","type":"application/atom+xml","href":"https://www.comrevo.com/feeds/4046296353839211293/comments/default","title":"Post Comments"},{"rel":"replies","type":"text/html","href":"https://www.comrevo.com/2019/08/Multi-threading-Synchronization-in-Java-with-Synchronized-Static-Method.html#comment-form","title":"0 Comments"},{"rel":"edit","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/4046296353839211293"},{"rel":"self","type":"application/atom+xml","href":"https://www.blogger.com/feeds/913600556879440043/posts/default/4046296353839211293"},{"rel":"alternate","type":"text/html","href":"https://www.comrevo.com/2019/08/Multi-threading-Synchronization-in-Java-with-Synchronized-Static-Method.html","title":"Multi-threading Synchronization in Java with Synchronized Static Method"}],"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"}}],"thr$total":{"$t":"0"}},{"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"}}]}});