8/18/2019 Playing With Threads
1/21
- Tausief Shaikh(Senior Server developer)
8/18/2019 Playing With Threads
2/21
IntroductionCovers sense of responsibility towards Project
development in IT
ocusin! on memory and CP" utili#ationsrather than just codin!$
Thread and Thread Pool
%clipse &ebu!!in!
"se of thread dumps and analysis usin! 'isual
8/18/2019 Playing With Threads
3/21
&evelopin! a software is easy but
developin! best software is indeed verytou!hIf you know how to code* have some basic lo!ic
thinkin!* and with proper assistance of +%T youcan develop applications easily$
%!, If you know 'ava core* like datatypes* Psconcepts* de.nin! classes* interfaces* variables*method* then you can code and !o on to developbi! applications$
/nowin! the write ways of codin! (desi!nin! inspeci.c) however needs some e0perience* butwould de.nitely come after spendin! some timein IT$
8/18/2019 Playing With Threads
4/21
&evelopin! a software is easy but
developin! best software is indeed verytou!h1%ven tou!h we develop an application* functionally all
correct* that doesn2t mean our work is done$ 3s adeveloper its our responsibility to make sure that theapplication works superb under heavy load$
4hen many users access the application concurrently*then this can add more load on the resources we use$
That means* more load on the memory* more CP" timeutili#ation* more disk space utili#ation* etc1$
ur application would always be deployed and run in a
limited environment* where we have some ma0memory si#e* ma0 disk space* ma0 capability of CP"core$ So there is always a challen!e of how manyre5uests the application can process in parallel*because beyond this the application would eventuallycrash$
8/18/2019 Playing With Threads
5/21
&evelopin! a software is easy but
developin! best software is indeed verytou!h1If many objects are created on heap for processin! a
sin!le* then when hu!e load hit the app in parallel* lot ofheap si#e would be occupied* and app would crash* soobjects has to be wisely allocated and cleaned-up frommemory$
6ike wise* when we create a thread* a new stack locationwould be allocated$ So if too many threads are created*then eventually a time would come when we mi!ht runout of memory crashin! the application a!ain$
6ike wise* we can risk the harddisk spaces as well$ Thus we should be very particular while accessin! these
resources* and should be a very valuable concern ofevery 'ava developer$
8/18/2019 Playing With Threads
6/21
Thread and Thread Pool4e all know that for every thread new stack memory is
allocated$ &ue to this* code runs in parallel$If you think like a server application developer(suppose
tomcat developer)* then for every incomin! re5uest* a
thread should be allocated* in order to process there5uest$
4e can simply create a new thread for every re5uest$%ven tou!h this would functionally be .ne* however would just be a ni!htmare when practically deployed on server$ne of the disadvanta!es of the thread-per-re5uest
approach is that the overhead of creatin! a new thread foreach re5uest is si!ni.cant7 a server that created a newthread for each re5uest would spend more time andconsume more system resources creatin! and destroyin!threads than it would processin! actual user re5uests$
8/18/2019 Playing With Threads
7/21
Thread and Thread PoolIn addition to the overhead of creatin! and
destroyin! threads* active threads consumesystem resources$ Creatin! too many threads in
one ' can cause the system to run out ofmemory or thrash due to e0cessive memoryconsumption$ To prevent resource thrashin!*server applications need some means of limitin!
how many re5uests are bein! processed at any!iven time$
To avoid this* the best solution is to introduceand maintain a ThreadPool$
8/18/2019 Playing With Threads
8/21
3dvanta!es of ThreadPool8y reusin! threads for multiple tasks* the thread-
creation overhead is spread over many tasks$ %!,when a re5uest comes* server would create many
threads and keep it ready for e0ecution$3s a bonus* because the thread already e0ists when
a re5uest arrives* the delay introduced by threadcreation is eliminated$ Thus* the re5uest can beserviced immediately* renderin! the application more
responsive$8y properly tunin! the number of threads in the
thread pool* you can prevent resource thrashin! byforcin! any re5uests in e0cess of a certain thresholdto wait until a thread is available to process it$
8/18/2019 Playing With Threads
9/21
Thread Pool 9 your usa!eIf you !et a opportunity to work on server
applications* then you can speed up yourresponse time and avoid memory crash issues$
3t times* you can tune the thread pool si#e of theserver* for suitin! your application re5uirement$
:ou can create your own thread pools forperformin! any asynchronous task$
or speci.c projects you can desi!n a frameworkover core threads and man!e it with pool in orderto control sync and async processin! of tasks$
8/18/2019 Playing With Threads
10/21
%clipse (with Thread)4hile a thread e0ecutes* it runs in Stack$ Stack
consists of ; parts, local variables* operandstack* and frame data$ The code instruction
e0ecutes one by one based on the lo!ic%!,
In this e0ecution ? -> ; -> @ ->1
%clipse contains a feature by which we can trackthe e0ecution
8/18/2019 Playing With Threads
11/21
Importance of eclipse
debu!!in! This can be the ultimate tool which can help
you in debu!!in! any major issues durin!project dev$
4hen you catch a thread* then you can seeentire internal details about the thread as towhat is actually happenin! at runtime of
project$ This can help you in e0plorin! runtime
8/18/2019 Playing With Threads
12/21
%clipse (with Thread) 9 &ebu!
Standalone appor debu!!in! a standalone 'ava application in
eclipse* follow the below process,
3dd breakpoint in java code where you want to stop
the thread e0ecution$ (double client on line numbersmentioned in left)
i!ht click 'ava class(with main())* &ebu! 3s D> 'ava3pp
%0ecutions stops at the line where breakpoint isadded
"sin! %clipse debu! perspective* walkthrou!h thread
8/18/2019 Playing With Threads
13/21
Startin! Tomcat in debu! mode 9
3pproach=&irectly con.!ure tomcat in eclipse$ Then E&ebu!
n ServerF would directly start tomcat in debu!mode and help your breakpoints !ettin! cau!ht$
3dvanta!e, This method is very useful whentomcat is runnin! in local machine* as it saves alot of manually eGorts and %cilipse does lot ofthin!s for you$
&isadvanta!e, 4hile workin! with sharedinte!rated environments* where tomcat is onanother linu0 machine* this process mi!ht not bethat handy
8/18/2019 Playing With Threads
14/21
Startin! Tomcat in debu! mode 9
3pproach? This approach would also be handly while
workin! with inte!rated environment
Step =, Start tomcat in debu! modeStep ?, %clipse Con.!uration to connect to
remote tomcat process
Continue1
8/18/2019 Playing With Threads
15/21
Startin! Tomcat in debu! mode 9
3pproach?Start tomcat in debu! modeHoto &S prompt, '33JK%> and run
below ? commandsset 'P&3J3&&%SSDLAAAset 'P&3JT3+SPTDdtJsocketun, '33JK%>MbinMcatalina$bat jpda start
%clipse con.!urationHoto &ebu! con.!urationIn left pane* start the remote 'ava application
pointin! to LAAA debu! port started by tomcatfor acceptin! remote debu! conections$
8/18/2019 Playing With Threads
16/21
Thread &ump and usa!e6ot of time durin! application testin!* you mi!ht
end up in a awkward situation where all your appthreads mi!ht be stuck or waitin! for somethin!$
3nd your application would not have any thin!threads available to process your re5uest$
In such cases* it really becomes tou!h to analy#eas to whatNs !oin! with the threads$ 3nd this
problems !rows with hu!e /6C(kilo lines ofcode)$ In entire code base* any wron!lyimplemented use-case* mi!ht have consumed allyour threads$
8/18/2019 Playing With Threads
17/21
Thread &ump and usa!e Threads mi!ht be E!ettin! stuckMconsumin! more
processin! timeF due to any of the below reasons,Synchroni#ed blocksMmethods
Takin! more time for &8 calls&ependencies on e0ternal resources* like KTTP calls or
remote webservices or remote ejbs+ested iterations can also add delay$r threads mi!ht be in sleepin! state$
4e should write code in such a way so as to optimi#eor avoid all of the above issues$ 8ut still* while doin!load testin!* you mi!ht end up observin! the aboveissues$
4hich risks thread utili#ations$
8/18/2019 Playing With Threads
18/21
Thread &ump and usa!e Thread &umps are used to monitor the state of
any of the application thread at a !iven point oftime$
Thread dump is a snapshot of all thread-state atany !ive point of time$
3t a !iven point of time you can say* whichthreads are doin! what in your application$
This can help you in identifyin! the bottleneckin your application$ Then you can visit the codeand work on the piece to make it perform better$
8/18/2019 Playing With Threads
19/21
Thread States - 'isual Threads can be in any of the below states"++I+H, currently threads are performin! some
tasks$ :ou can see currently the thread is runnin!
which line of which class$S6%%PI+H, Indicates currently thread is in sleepin!
state43ITI+H, This doesnNt specify problem situation* it
simply means that thread is available in pool and
can process any new task which is submitted topool$
+IT, Currently thread is blocked due to lockac5uired by other thread on a resource which thisthread is tryin! to active$
8/18/2019 Playing With Threads
20/21
'ava isual 'isual is a tool which comes alon! with java
installation under '33JK%>Mbin folder
This can be used to indicate internal memory
details of an application$ 6ike CP" utili#ations*Keap occupancy* classes and thread details in thememory$
:ou can eGectively check the thread state of the
system$It provides with a EThreads inspectorF window*
which helps you in identifyin! at which linenumber the threads are currently$
8/18/2019 Playing With Threads
21/21
Thank :ou$
Presenter details,
+ame, Tausief Shaikh
%mailId, tausiefshaikh=O!mail$com
b,https,MMwww$facebook$comMtausiefs
Contact,
mailto:[email protected]://www.facebook.com/tausiefshttps://www.facebook.com/tausiefsmailto:[email protected]