Friday 10 February 2017

MultiThreaded Programming in Java ( PART - I )


                                                     
MultiThreaded Programming in Java ( PART - I )
 

A MultiThreaded program  contains 2 or more parts that can run concurrently. Each part of the program is called as thread and each thread defines a separate path of execution. Thus , it is also called as specialized form of Multitasking.

There are 2 type of  MultiTasking :

- Process  Based
- Thread Based

Process Based : 

 A process  is the program that is executing. Thus , process based multitasking is the feature that allows your computer to run 2 or more programs concurrently. 

In this , a program is the smallest unit of code that can be dispatched by the scheduler.

Thread Based :

In this , a Thread is the smallest unit of the dispatchable code. This means that single program can perform 2 or more tasks simultaneously.

The Java Thread Model

The java runtime system depends on threads for many things and java uses threads to enable the entire environment to be asynchronous. This help reduce in efficiency by preventing the waste of CPU cycles.

In  java thread model , the main loop / polling system is eliminated , which was main component of single thread system and thus made it inefficient.

In java's thread model ,  , 1 thread can  pause  without stopping other parts of your program. When a thread blocks in java program , only the single thread that is blocked pauses , all other threads continue to run.

The threads has 6 states :

- Running  : when is currently running.
- Ready To Run :  When it gets CPU time .
- Suspended  : Temporarily suspends it's activity.
- Resumed : suspended thread picks up from where it was left off.
- Blocked : Waiting for a Resource.
- Terminated   : Halts the Execution. 

Thread Priorities

Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread priorities are integers that specify the relative priority of 1 thread to another.

A thread priority is used  to decide when to switch from 1 running thread to the next. This is called Context Switch. The rules are :

- A thread can voluntarily relinquish control

This is done by explicitly yeilding , sleeping or blocking on pending I/O.

- A thread can be preempted by a higher priority thread

 a lower priority thread that does not yield the processor is simply preempted , no matter what it is doing.


                                                                       By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *