Multithreading in java

1,687 views 15 slides Dec 02, 2018
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

Basic of java multithreading. Thread and How to create threads. Advantages of multithreading.


Slide Content

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. r un(): 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. n otify( ):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 I nterface: 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 : public static int MIN_PRIORITY(1) public static int NORM_PRIORITY(5) 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 .

THANK YOU