TOPIC:
Multithreading
What is Multithreading?
Multithreading in java is a process of
executing multiple threads simultaneously.
It is a conceptual programing paradigm
where a program is divided into two or
more subprograms which can be
implemented at the same time.
Life Cycle Of Thread
Life Cycle Of Thread(in details)
Newborn : When we create a new Thread
object using new operator , thread state is New
Thread .At this point thread is not alive and it’s
a state internal to java program.
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 it’s execution.
Running: When thread is executing, it’s state is
changed to Running. A thread can change state to
Runnable, Dead or Blocked from running state
depends on time slicing, thread completion run()
method or waiting for some resources.
Blocked /Waiting: The programmer can make a
running thread to become inactive temporarily
for some period. In this period, the thread is
said to be in blocked state.
Dead : When the execution of run() method is
over, as the job it is meant is done, it is brought
to dead state.
Some Thread Method
Start(): Creates a new thread and make it runnable.
run(): It is used to perform action for a thread.
yield(): causes the currently executing thread object to
temporarily pause and allow other threads to execute.
suspend(): is used to suspend the thread
resume(): is used to resume the suspended thread
Sleep():A thread to sleep for specified time period using
the method sleep.
stop(): is used to stop the thread.
Wait():it is use a thread inactive foe some time.
notify():it use to resume the waited thread.
Example
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
How to create thread
There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
Thread Class: Thread class provide constructors and
methods to create and perform operations on a thread.
Thread class extends Object class and implements
Runnable interface. Example:
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
} }
Runnable Interface: The Runnable interface should be
implemented by any class whose instances are
intended to be executed by a thread. Runnable
interface have only one method named run().
Example:
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running..."); {
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
} }
Priority of a Thread
Each thread have a priority. Priorities are
represented by a number between 1 and 10. In most
cases, thread scheduler schedules the threads
according to their priority (known as preemptive
scheduling).
Type of priority:
1.public static int MIN_PRIORITY(1)
2.public static int NORM_PRIORITY(5)
3.public static int MAX_PRIORITY(10)
Priority of a Thread(Example)
class TestMultiPriority1 extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
} }
Advantages of Java Multithreading
1) It doesn't block the user because threads
are independent and you can perform
multiple operations at the same time.
2) You can perform many operations together,
so it saves time.
3) Threads are independent, so it doesn't
affect other threads if an exception occurs in
a single thread.
Multithreading in java

Multithreading in java

  • 1.
  • 2.
    What is Multithreading? Multithreadingin java is a process of executing multiple threads simultaneously. It is a conceptual programing paradigm where a program is divided into two or more subprograms which can be implemented at the same time.
  • 3.
  • 4.
    Life Cycle OfThread(in details) Newborn : When we create a new Thread object using new operator , thread state is New Thread .At this point thread is not alive and it’s a state internal to java program. 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 it’s execution.
  • 5.
    Running: When threadis executing, it’s state is changed to Running. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion run() method or waiting for some resources.
  • 6.
    Blocked /Waiting: Theprogrammer can make a running thread to become inactive temporarily for some period. In this period, the thread is said to be in blocked state. Dead : When the execution of run() method is over, as the job it is meant is done, it is brought to dead state.
  • 7.
    Some Thread Method Start():Creates a new thread and make it runnable. run(): It is used to perform action for a thread. yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute. suspend(): is used to suspend the thread resume(): is used to resume the suspended thread Sleep():A thread to sleep for specified time period using the method sleep. stop(): is used to stop the thread. Wait():it is use a thread inactive foe some time. notify():it use to resume the waited thread.
  • 8.
    Example class Multi extendsThread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
  • 9.
    How to createthread There are two ways to create a thread: 1.By extending Thread class 2.By implementing Runnable interface.
  • 10.
    Thread Class: Threadclass provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. Example: class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
  • 11.
    Runnable Interface: TheRunnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run(). Example: class Multi3 implements Runnable{ public void run(){ System.out.println("thread is running..."); { public static void main(String args[]){ Multi3 m1=new Multi3(); Thread t1 =new Thread(m1); t1.start(); } }
  • 12.
    Priority of aThread Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread scheduler schedules the threads according to their priority (known as preemptive scheduling). Type of priority: 1.public static int MIN_PRIORITY(1) 2.public static int NORM_PRIORITY(5) 3.public static int MAX_PRIORITY(10)
  • 13.
    Priority of aThread(Example) class TestMultiPriority1 extends Thread{ public void run(){ System.out.println("running thread name is:"+Thread.currentThread().getName()); System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); } public static void main(String args[]){ TestMultiPriority1 m1=new TestMultiPriority1(); TestMultiPriority1 m2=new TestMultiPriority1(); m1.setPriority(Thread.MIN_PRIORITY); m2.setPriority(Thread.MAX_PRIORITY); m1.start(); m2.start(); } }
  • 14.
    Advantages of JavaMultithreading 1) It doesn't block the user because threads are independent and you can perform multiple operations at the same time. 2) You can perform many operations together, so it saves time. 3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.