24 Sept 2012

advantages and disadvantages of threads in java

/* advantages and disadvantages of threads in java, advantages and disadvantages of using threads in java */

Advantages:


Some advantages include:

     1. Reduces development time.
     2. Reduces maintenance costs.
     3. Improves the performance of complex applications.
     4. Useful for improving the responsiveness of the user interfaces.
     5. Used in server applications for improving high throughput and resource utilization.
     6. Parallelize tasks.
    7. If a thread cannot use all the computing resources of the CPU (because instructions depend on each other's result), running another thread can avoid leaving these idle.
    8. If several threads work on the same set of data, they can actually share their cache, leading to better cache usage or synchronization on its values.

Disadvantages:


Some disadvantages include:


    1. Multiple threads can interfere with each other when sharing hardware resources such as caches or translation lookaside buffers (TLBs).

      2. Execution times of a single thread are not improved but can be degraded, even when only one thread is executing. This is due to slower frequencies and/or additional pipeline stages that are necessary to accommodate thread-switching hardware.
    3. Hardware support for multithreading is more visible to software, thus requiring more changes to both application programs and operating systems than multiprocessing.

Use of thread in java:


For example, You have a Java program to compute the sum of two numbers:


public class sumoftwonumbers

{
   public static void main(String[] args) 
     {
     int a=Integer.parseInt(args[0]);
     int b=Integer.parseInt(args[1]);
     System.out.print("Sum of "+a+" "+b+" is: ");
     int c=a+b;
     System.out.println(c);
     }
}

When you run this program, it executes a sequence of instructions, that list of instructions looks like this:


1. convert args[0] to an integer and store that integer in a location called 'a'.

2. convert args[1] to an integer and store that integer in a location called 'b'.
3. print some text.
4. calculating the sum of two numbers called 'a' and 'b', and store it in a location called 'c'.
5. print out the value stored in 'c'.

This program is executed as a series of instructions, the execution path of these instructions is called as a thread.


In your program, that thread is called as main thread, and it begins executing statements with the first statement of the main() method of your class.


If there are two separate tasks in your java program, so you could choose to write them as two separate threads.


What is multiprocessing and multithreading?

Multiprocessing: If two or more programs are executing concurrently is called as multiprocessing.
Multithreading: If two or more tasks are executing concurrently is called as multithreading.

For ieee projects visit our site here

or Visit our blog

FatCow Plan for $3.15/mo. only

5 comments:

Anonymous said...

nice tutorial.....

Diya shree said...

Hi, Thanks a lot for your explanation which is really nice.Thanks for your blogs that are very helpful to learn the things .
To know more about learning /training institute , view this link http://www.bestjavatraining.in

Unknown said...

Tqq nice tutorial

IT said...

I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting java online training hyderabad

Yozam said...

Thanks, helpful content!

Post a Comment