Вы находитесь на странице: 1из 9

THREAD

▪ Facilities to allow multiple activities to coexist within a single


process.
▪ A thread is light weight sub process, smallest unit of sub-processing.
▪ Threads are independent.
▪ A thread is a single sequential flow of control within a program.
▪ Threads of same process share same memory space.
MULTITHREADING
Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU.
Each part of such program is called a thread.
So, threads are light-weight processes within a process.

APPLICATION OF MULTI THREAD


-multimedia graphics
-animations
-video games
-web servers
-application servers
LIFE CYCLE OF THREAD
▪ A thread can be in one of the five states.
▪ The life cycle of the thread in java is controlled by JVM. The java thread
states are as follows:
1.New
2.Runnable
3.Running
4.Non-Runnable (Blocked)
5.Terminated
JAVA PROGRAM

RUNNABLE

NEW THREAD
BLOCKED/WAITING
RUNNING

START THREAD
DEAD
NEW
When we create a new Thread object using new operator, thread state is New thread.
RUNNABLE
When we call start() function on Thread object, it’s state is changed to runnable . The control is given to
Thread scheduler to finish its execution.
RUNNING
When thread is executing , it’s state is changed to Running . Thread scheduler pick one of the thread from
the runnable thread pool and change it’s state to running . The CPU starts executing this thread.
BLOCKED/WAITING
A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to
available.
DEAD
Once the thread finished executing, it’s state is changed to dead and it’s considered to be not alive.
HOW MANY WAYS TO CREATING THREAD
1. By extends thread class
Create a thread by a new class that extends thread class and instance of class . Extending
class must overrides run() method which is the entry point of thread.
For example –
public class mythread extends thread
{
public void run()
{
System.out.print(“thread is start to running………”);
}
public static void main(String[]args)
{
mythread mt = new mythread;
Mt.start();
}}
2.By implementing runnable interfaces
This is the easiest way to create thread by implementing runnable interface . After
runnable interface, class needs to implements the run() method which is, public void run().
For example –
public class mythread extends thread
{
public void run()
{
System.out.print(“thread is start to running………”);
}
public static void main(String[]args)
{
mythread mt = new mythread;
thread t = new thread;
t.start();
}}
MAIN THREAD
When we start any java program, one thread begins running immediately, which is called
Main Thread of that program.
The main thread is created automatically when your program is started.

class Shweta
{
public static void main(String[]args)
{
System.out.print(“main thread”);
}
}
HOW TO START THREAD
class Shweta
{
public static void main(String[]args)
{
mythread t = new mythread();
t.start();
for(int i=0;i<5;i++)
{
System.out.print(“main thred”);
}
}
}

Вам также может понравиться