Thursday, February 10, 2011

Multithreading in Java

Multithreading in Java

Some times we do multple tasks at a particular period of time to complete the unit of work ASAP.
For example: If your working on one issue in the middle you came to know that this issue is dependent on DB. You need help from
DB guy if DB guy is not available at this point of time, you will not keep waiting for him. you will switch to some other task to work on.
ones he come back you can finish the task which is dependent on DB.

Java also perform muti tasking using Multithreading, so that it will improve the performance of the appilication.

We can see how to implement multithreading in java.

In Java We have two types of multi tasking.
1. Thread based multi tasking.
2. Process based multi tasking.

Processed based multi threading:
The best way to remeber is Multiple programs running at a time
ex: adding and subtracting.

Thread based multi tasking.

Multiple tasks in one program.
if we take one example adding numbers 1+2+3+4=9

if one thread is processing this program if we assume for each operation it is taking 1ms it will take 3ms to add this .
if we perform multithreading first two numbers will add by one thread and the last two numbers will add thread this task will in 1 sec by using multi threading
the result of two threads can added in another secong we can complete this in two seconds, so we are able to save 33% of the time.


Java run time environment manages threads, Threads are processed asynchrously this means that one thread can wait while other thread can continue process.
A thread can be in one of four states:
• Running A thread is being executed.
• Suspended Execution is paused and can be resumed where it left off.
• Blocked A resource cannot be accessed because it is being used by
another thread.
• Terminated Execution is stopped and cannot be resumed.

Thread Priority: In a mutlithreaded programing few threads can be dependent on the result of anthor thread so by giving the priority
we can decide which thread have to excute first.





We can see how to implement multithreading in java.

Creating Thread
Multiple Threads in a Program
Synchronization Concept
Thread Communication

No comments:

Post a Comment