Monday 18 April 2011

multithreading


How does multithreading improve the performance of Java?
http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
Since multiple actions happen parallel (almost/virtually) to one another rather than one after the other, multithreading improves performance.

Ex: lets say there are three actions each that will take 5 seconds.

so, in a single threaded environment it will take atleast 15 seconds if they happen one after the other.

But, if we spawn three threads and have these actions started at the same time, all 3 will complete probably in around 6 or 7 seconds which is much faster than the earlier 15 seconds it took.

What advantages of multithreading in java?

http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
The advantage is the fact that multiple threads run parallel to one another and hence things happen faster than they would if they run one after the other

What is the Life cycle diagram of Thread in Java? Different states of thread?
http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
Different states of a thread are :

New state - After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.

Runnable (Ready-to-run) state - A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.

Running state - A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.

Dead state - A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.

Bocked - A thread can enter in this state because of waiting the resources that are hold by another thread.

No comments:

Post a Comment