Pages

Monday 14 October 2013

Multithreading in Java

Multithreaded Program:

           A program that contains two or more threads and these threads are capable of running concurrently. The multithreading is one of the specialized from of multitasking. 

   What is thread?

         A thread is a smaller group of codes in a program and every thread in a program defines a different path of execution. 

Multitasking: 

         The mechanism by which two or more tasks are carried out concurrently in a system is called multitasking. 

         The multitasking can be subdivided into two as mentioned below

1.      Process-Based Multitasking.

2.      Thread-Based Multitasking.

        Process-Based multitasking allows a system to run two or more tasks simultaneously. For example, consider your PC with windows XP, 7 or 8 OS, in which it is possible to listen to song using the media player and simultaneously play games or chat with your friends by using messengers like yahoo, etc. You can run all this software simultaneously because Windows OS installed in your PC supports multitasking.

Note: In process-based multitasking, a program is the smallest unit of code that will be dispatched.

       In Thread-Based multitasking environment, a program can do two or more tasks simultaneously. For example consider MS Word text editor where you can edit text or format text and print the document simultaneously. This is possible because text editing is carried out by one thread and printing action is carried out by the other thread. 

Note: In thread-based multitasking, the thread is the smallest unit of code that will be dispatched.
      The thread-based and process-based multitasking are distinguished in the below table.

Process-Based
Thread-Based
More overhead
Less overhead
Processes are heavy weight tasks. They require their own separate address space
Threads are light weight tasks. They will share the same address space and also the same heavy weight process
The inter-process communication is limited and expensive
The inter-thread communication is not expensive
The context switching between processes is costly.
The context switching between threads is easy and low cost.

Threads in Java:

       The Java is designed so that it supports multithreading efficiently. The Java’s run-time system depends on thread to do many tasks. 

       In Java, if a thread stops its execution then no harm is caused to the other part of the program. The thread in Java can be in any of the below mentioned states.

1.      Running state
2.      Ready to run state
3.      Resumed state
4.      Suspended state
5.      Blocked state
6.      Terminated

        Each thread in Java can be given priority. The thread priorities are integer that decides, how threads have to be treated with respect to one another.

        Thread priority are used to decide when to switch a thread from the next thread. This switching of thread is called context switching.

        When context switching takes place?

        In the below mentioned conditions the context switching takes place.

1.      A thread can voluntarily surrender control.

2.      A thread can be preempted by a high-priority thread.

3.      The threads having same priority will create a little complexity. In OS like windows, the threads of same priority are time sliced & handled in round-robin way. In other OS equal priority threads must voluntarily give up control to their peer threads.

Note: In Java, the integer values of minimum and maximum priority of threads are 1 and 10 respectively. The default value of thread in Java is 5.

        How to implement threads in Java?

        In two ways you can implement threads in Java.

1.      By extending Thread class.

2.      By implementing Runnable interface.

        The thread class defines several methods. The below table list some of the important methods defined by thread class.
                       
Method
currentThread
getName
getPriority
isAlive
join
run
sleep
Start