Friday 17 August 2012

What is multithreading in Java ?

A multithreading program contains two or more parts that can run concurrently. Each part of such a program   is called a thread and each thread defines a seperate path of execution. Thus multithreading is a specialized form of multitasking.

However there are two distinct types of multitasking :
1. Process based       
2. Thread based

It is important to understand the difference between these two : 
Process based multitasking is the feature that allows your compiler to run two or more programs concurrently. Example. Process based multitasking enables you to run the Java compiler at the same time when you are using a text editor.
In thread based multitasking, a single program can perform two or more tasks simultaneously.

Thread Priorities :
Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. A thread priority is used to decide when to switch from one running thread to the next. This is called the context switch.
  •  A thread can voluntarily relinquish control. This is done by explicitly locking or sleeping on pending input output. In this scenario all other threads are examined and the highest priority thread that is ready to run is given the CPU.
  • A thread can be preemted by a higher priority thread.

Java defines two ways of creating a thread :
  1. You can implement runnable interface .
  2. You can extend the thread class itself.

Now follow :


No comments:

Post a Comment