Thread
A thread is similar to a program that has a single flow of control. A thread has a beginning, a boy, and an end, and executes commands sequentially. Every program will have at least one thread.
Multithreading:
Multithreading is a conceptual paradigm where a program (or a process) is divided into two or more subprograms (or processes), which can be implemented at the same time in parallel. For example, one subprogram 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 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 (or module) known as a thread that runs in parallel to others.
A program that contains multiple flows of control is known as a 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 heart and soul of any thread. It makes up the entire body of a thread and is the only method in which the thread’s behavior can be implemented. A typical run method appears as follows:
public void run()
{
……………..
………………(statements for implementing threads)
……………..
}
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 thread method called start().
A new thread can be created in two ways:
1) By Creating a thread class: Define a class that extends Thread class and overrides its run() method with the code required by the threa.
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:
First of all import the thread package by importing java.lang.Thread . This gives us access to all the thread methods directly.It includes the following steps:
(i) Declare the class as extending the Thread Class.
Class demoThread extends Thread
{
…….
…….//statements
…….
}
(ii) Implement the run() method that is responsible for executing the sequence of code that the thread will execute.
Public void run()
{
…….
//thread code here
…….
}
(iii) Create a thread object and call the start() method to initiate the thread execution.
Starting New Thread:
To actually create and run an instance of our thread class, we must write the following:
MyThread objThread = new MyThread;
objThread.start(); //invokes run() method
Example:
Class A extends Thread
{
Public void run()
{
for(int i=1 ; i<2 ; i++)
{
System.out.println(“\tFrom Thread A: i = ”+i);
}
}
System.out.println(“Exit from A”);
}
Class B extends Thread
{
Public void run()
{
for(int i=1 ; i<2 ; i++)
{
System.out.println(“\tFrom Thread B: i = ”+i);
}
}
System.out.println(“Exit from B”);
}
Class ThreadTest
{
Public static void main(String args[])
{
new A().start();
}
}
Starting New Thread:
To actually create and run an instance of our thread class, we must write the following:
MyThread objThread = new MyThread;
objThread.start(); //invokes run() method
Example:
Class A extends Thread
{
Public void run()
{
for(int i=1 ; i<2 ; i++)
{
System.out.println(“\tFrom Thread A: i = ”+i);
}
}
System.out.println(“Exit from A”);
}
Class B extends Thread
{
Public void run()
{
for(int i=1 ; i<2 ; i++)
{
System.out.println(“\tFrom Thread B: i = ”+i);
}
}
System.out.println(“Exit from B”);
}
Class ThreadTest
{
Public static void main(String args[])
{
new A().start();
}
}
IMPLEMENTING THE ‘RUNNABLE INTERFACE’ :
The runnable interface declares the run() method that is required for implementing threads in our
programs. To do this, we must perform the following steps;
1. Declare the class as implementing the runnable interface.
2. Implement the run() method.
3. Create a thread by defining an object that is instantiated from this “runnable” class as the
target of the thread.
4. Call the thread’s start() method to run the thread.
EXAMPLE:
Class X implements Runnable
{
Public void run()
{
for(int i=1 ; i<=5 ; i++)
{
System.out.println(“\t ThreadX : ”+i);
}
System.out.println(“End of ThreadX”);
}
}
Class RunnableTest
{
Public static void main(String args[])
{
X runnable = new X();
Thread threadX = new Thread(runnable);
thread.start();
System.out.println(“End of main Thread”);
}
No comments:
Post a Comment