by Dinesh Thakur

Multithreading is a powerful programming tool that makes it possible to achieve concurrent execution of multiple units of a program. Each portion of a program-designated a thread-may execute concurrently with others. Multithreading basically enables a program to do more than one task at a time and also to synchronize these tasks. Java builds thread support directly into the language. Multithreading support for Java includes thread creation, thread prioritizing, thread scheduling, resource locking (thread synchronization) and establishing inter-thread communication.

 

Define Threads

 

A thread is a single sequential flow of control within a program. It differs from a process in that a process is a program executing in its own address space whereas a thread is a single stream of execution within a process.

 

A thread can be defined as a process in execution within a program.

 

A thread by itself is not a complete program and cannot run on its own. It runs within a

 

program; however each thread has a beginning, and an end, and a sequential flow of execution at a single point of time. At any given instant within the run-time of the program, only one thread will be executed by the processor.

 

Multithreading allows a program to be structured as a set of individual units of control that run in parallel; however, the Central Processing Unit (CPU) can only run one process at a time. The CPU time is divided into slices and a single thread will run in a given time slice. Typically, the CPU switches between multiple threads and runs so fast that it appears as if all threads are running at the same time. It is better, as a programming practice, to identify different parts of the program that can perform in parallel and implement them into independent threads.

 

Multithreading differs from multitasking: multitasking allows multiple tasks (which can be processes or programs) to run concurrently whereas multithreading allows multiple units within a program (threads) to run concurrently.

 

Unlike the processes in some operating systems (for example, Unix), the threads in Java are 'light-weight processes' as they have relatively low overheads and share common memory space. This facilitates an effective and inexpensive communication between threads.

Example of Thread

class NewThreadDemo implements Runnable

{

         Thread t;

         NewThreadDemo(String threadName)

      {

          t=new Thread(this, threadName);

          System.out.println("New userdefined child thread created" +threadName);

          t.start();

       }

 

         public void run()

       {

           try

            {

                  for(int i = 1; i <=10; i++)

                     {

                         System.out.println("Child Thread: [" +Thread.currentThread().getName()+ "] " + i);

                            Thread.sleep(500);

                      }

             }

                  catch (InterruptedException e)

                     {

                          System.out.println("UseDefine Child interrupted.");

                      }

                          System.out.println("Exiting UserDeinfed child thread.");

         }

}

class MultiThreadDemo

{

               public static void main(String args[])

      {

                new NewThreadDemo("Child_One");

                new NewThreadDemo("Child_Two");

                new NewThreadDemo("Child_Three");

                new NewThreadDemo("Child_Four");

            try

              {

                     for(int i = 1; i <10; i++)

                        {

                            System.out.println("Main Thread: " + i);

                            Thread.sleep(500);

                         }

               }

                  catch (InterruptedException e)

                         {

                                System.out.println("Main thread interrupted.");

                          }

                                System.out.println("Main thread exiting.");

         }

}

 

Output

 

Multithreading