Multithreaded Programming

Multithreading is a conceptual programming paradigm where a program is divided into two or more sub-programs which can be implemented at the same time in parallel.For example one sub-program can display an animation on the screen while another may build the next animation to be displayed.This is something similar to dividing a task into subtasks and assigning them to different people for execution independently and simultaneously.

                       A Thread is similar to a program that has a single flow of control.It has a beginning,a body and an end and executes command sequentially.A unique property of java is its support for

multithreading.That is,java enables us to use multiple flows of control in developing programs. Each flow of control may be thought of as a separate tiny program known as thread that run in parallel.A program that contain multiple control flow is known as Multithreaded program.

Creating Threads:- creating threads in java is simple.threads are implemented in the form of objects that contain a method called run().The run() method is the major part of any thread.it makes up the entire body of a thread and is the only method in which the thread’s behaviour can be implemented.

Syntax to Write run() Method:-

 

public void run()

{

……………………..

……………………..

}

The run() method should be invoked by an object of the concerned thread.This can be achieved by creating the thread and initiating it with the help of another method called start().

A new thread can be created n two ways:-

1.) By Creating A Thread Class:-  Define a class that extends Thread class and override its run() method  with the code required by the thread.

2.) By Converting a class to a thread:-  define a class that implements Runnable interface.The Runnable interface has only one method,run() that is to be defined in the method with the code to be executed by the thread.

Extending The Thread Class:-  We can  make our class runnable as thread by extending the class java.lang.Thread.This gives us access to all the thread methods directly.It can be done by following steps:-

1.) Declare the class as extending the Thread class.

2.) Implement run() method that is responsible for executing the sequence of the code that the thread will execute.

3.) Create a thread object and call the start() method to initiate the execution.

 

Example Program:-


class A extends Thread

{

public void run()

{

for(int i=0;i<=5;i++)

{

System.out.println(“Thread A running i=” + i);

}

System.out.println(“Thread A exits”);

}

}

class B extends Thread

{

public void run()

{

for(int j=0;j<=5;j++)

{

System.out.println(“Thread B running j=” + j);

}

System.out.println(“Thread B exits”);

}

}

class C extends Thread

{

public void run()

{

for(int k=0;k<=5;k++)

{

System.out.println(“Thread C running k=” + k);

}

System.out.println(“Thread C exits”);

}

}

public class threadtest

{

public static void main(String args[])

{

A a1=new A();

B b1=new B();

C c1=new C();

System.out.println(“Main thread Starts”);

System.out.println(“Thread A Starts”);

a1.start();

System.out.println(“Thread B starts”);

b1.start();

System.out.println(“thread c starts”);

c1.start();

System.out.println(“thread Main ends”);

}

}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s