Playing With Threads: Tausief Shaikh
Playing With Threads: Tausief Shaikh
Threads
Tausief Shaikh
(Senior Server developer)
Introduction
Covers sense of responsibility towards Project
development in IT
Focusing on memory and CPU utilizations
rather than just coding.
Thread and Thread Pool
Eclipse Debugging
Use of thread dumps and analysis using
JVisualVM
Developing a software is easy but developing
best software is indeed very tough
If you know how to code, have some basic
logic thinking, and with proper assistance of
NET you can develop applications easily.
Eg: If you know Java core, like datatypes,
OOPs concepts, defining classes, interfaces,
variables, method, then you can code and go
on to develop big applications.
Knowing the write ways of coding (designing
in specific) however needs some experience,
but would definitely come after spending
some time in IT.
Developing a software is easy but developing
best software is indeed very tough
Even tough we develop an application, functionally all correct,
that doesn't mean our work is done. As a developer its our
responsibility to make sure that the application works superb
under heavy load.
When 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 CPU time utilization, more
disk space utilization, etc.
Our application would always be deployed and run in a limited
environment, where we have some max memory size, max
disk space, max capability of CPU core. So there is always a
challenge of how many requests the application can process in
parallel, because beyond this the application would eventually
crash.
Developing a software is easy but developing
best software is indeed very tough
If many objects are created on heap for processing a
single, then when huge load hit the app in parallel, lot of
heap size would be occupied, and app would crash, so
objects has to be wisely allocated and cleaned-up from
memory.
Like wise, when we create a thread, a new stack location
would be allocated. So if too many threads are created,
then eventually a time would come when we might run out
of memory crashing the application again.
Like wise, we can risk the harddisk spaces as well.
Thus we should be very particular while accessing these
resources, and should be a very valuable concern of every
Java developer.
Thread and Thread Pool
We all know that for every thread new stack memory is
allocated. Due to this, code runs in parallel.
If you think like a server application developer(suppose tomcat
developer), then for every incoming request, a thread should
be allocated, in order to process the request.
We can simply create a new thread for every request. Even
tough this would functionally be fine, however would just be a
nightmare when practically deployed on server.
One of the disadvantages of the thread-per-request approach is
that the overhead of creating a new thread for each request is
significant; a server that created a new thread for each request
would spend more time and consume more system resources
creating and destroying threads than it would processing
actual user requests.
Thread and Thread Pool
In addition to the overhead of creating and
destroying threads, active threads consume
system resources. Creating too many threads in
one JVM can cause the system to run out of
memory or thrash due to excessive memory
consumption. To prevent resource thrashing,
server applications need some means of limiting
how many requests are being processed at any
given time.
To avoid this, the best solution is to introduce
and maintain a ThreadPool.
Advantages of Thread
Pool
By reusing threads for multiple tasks, the thread-
creation overhead is spread over many tasks. Eg: when
a request comes, server would create many threads and
keep it ready for execution.
As a bonus, because the thread already exists when a
request arrives, the delay introduced by thread creation
is eliminated. Thus, the request can be serviced
immediately, rendering the application more responsive.
By properly tuning the number of threads in the thread
pool, you can prevent resource thrashing by forcing any
requests in excess of a certain threshold to wait until a
thread is available to process it.
Thread Pool your usage
If you get a opportunity to work on server
applications, then you can speed up your
response time and avoid memory crash
issues.
At times, you can tune the thread pool size of
the server, for suiting your application
requirement.
You can create your own thread pools for
performing any asynchronous task.
For specific projects you can design a
framework over core threads and mange it
Eclipse (with Thread)
While a thread executes, it runs in Stack. Stack
consists of 3 parts: local variables, operand stack,
and frame data. The code instruction executes
one by one based on the logic
Eg:
Presenter details:
Name: Tausief Shaikh
EmailId: [email protected]
Fb: https://www.facebook.com/tausiefs
Contact: