Multithreaded 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 . Threads allow the program to perform multiple tasks simultaneously Thus multithreading is a specialised form of multitasking. There are Two types of Multitasking.
1. Process based Multitasking -> to run one or more programs simultaneously .
2. Thread based Multitasking-> Single Program to run one or more tasks or parts simultaneously
in Detail
---------
A process is a program that is executing. Every operating system supports the concept of processes .Process based multitasking is the feature that allows your computer to run two or more programs. For e.g. Running the Java Complier + Using Text Editor simultanious. In a Thread based multitasking, Thread is a smallest unit of dispatchable code. That is the single program can run one or more tasks simultaneously. e.g.) Text Editor Formatting + Printing simultaneously.
Creating Threads in Java
------------------------
Threads can be accomplished by two ways
a) Extending Thread Class
b) Implementing Runnable Interface.
Every program is has atleast one thread. that is main thread.Main thread is started automatically when a java program is started to run. However you can control the main thread using Thread Object.
class mainThread
{
public static void main(String args[])
{
Thread t= Thread.currentThread(); // Gets the reference of the current Thread which is running and stores in a variable
System.out.println("Thread Started"+t);
t.setName("Name Change Thread");
System.out.println("Thread Name after name change"+t);
System.out.println("Priority" + t.getPriority());
try
{
for (int i=5;i>0;i--)
{
System.out.println(i);
Thread.sleep(1000);
}
} catch (InterruptedException e)
{
System.out.println("Thread Interrupted");
}
}
}
Some of the Methods of Thread Class
------------------------------------
1) getName() - Returns Thread's name
2) getPriority() ; Returns the Thread's Priority
3) isAlive ; to determine if a thread is still running.
4) join ; Wait for a thread to terminate
6) run ; this is a funtion which is an Entry point for the thread.
7) start ; Start a thread by calling its run method.
8) sleep ; Suspend a thread for a period of time.
Example for Implementing Runnable
---------------------------------
Create a threadStart.java file
-------------------------------
public class threadStart
{
public static void main(String args[])
{
System.out.println("Main Thread Started");
threadExample te=new threadExample();
te.childThread();
System.out.println("Main Thread End");
}
}
class threadExample implements Runnable
{
public void childThread()
{
System.out.println("Before Child Thread Start") ;
Thread t=new Thread(this, "demo thread");
t.start();
}
public void run()
{
try
{
for (int i=5; i>0; i--)
{
System.out.println("Child Thread Started Thread "+ i);
}
} catch (InterruptedException e)
{
System.out.println("child thread interrupted");
}
}
}
0 comments:
Post a Comment