Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithreading in Java

Similar presentations


Presentation on theme: "Multithreading in Java"— Presentation transcript:

1 Multithreading in Java

2 Contents Introduction to threads and multi-threading Thread Creation
Extending Thread class Implementing Runnable interface Creating multiple threads Thread states Thread synchronization Thread scheduling and priority

3 Introduction to Threads and Multi-threading
Threads are light weight processes. A thread is a path of code execution through a program. Thread consists of: Its own local variables Program counter Lifetime In every Java program, we’ve at least one thread running which is the “main” thread. In a multithreaded environment, you can create more than one thread inside an application.

4 Introduction to Threads and Multi-threading (continued…)
Figure showing single threaded and multi-threaded programming

5 Thread Creation Every thread of execution, in Java, begins as an instance of class Thread. Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Threads can be created in any of the two known ways: By extending the Thread class. By implementing the Runnable interface. Runnable interface contains a single method run() You need to define run() method while creating the thread in any of the two ways.

6 Thread Creation (continued…)
Thread creation by extending java.lang.Thread class Thread creation by implementing Runnable interface

7 Thread Creation Extending Thread Class
Thread can be created by extending the java.lang.Thread class It’s considered as the easiest method among the two. It restricts the programmer from extending any other class. Four steps should be followed to create thread in this way: Create a class extending java.lang.Thread class. Override java.lang.Thread’s run() method in your class. Instantiate the thread by instantiating your class. Invoke the start() method that your class inherited from the Thread class.

8 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();    }  

9 Thread Creation Implementing Runnable Interface
Thread can be created by implementing java.lang.Runnable interface. This provides the programmer liberty to extend any class. Four steps needs to followed to create thread in this way: Create a class implementing the Runnable interface. Implement the run() method of Runnable interface your class. Create an object of the Thread class by passing an instance of the above created class. Invoke the start() method on your Thread object.

10 class Multi3 implements Runnable{ public void run(){ System. out
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(); }

11 Creating Multiple Threads
There are situations where you need to same job multiple times or do multiple jobs simultaneously. Multiple threads is a good option in such situations. CPU cycles are shared among the multiple threads. Each thread runs in it’s own call stack.

12 4.3 Thread States: Life Cycle of a Thread

13 Thread Synchronization
Multithreading, if not monitored, results in asynchronous behavior to the programs. To protect shared resource from asynchronous access, thread should be synchronized using locks. Only one thread can hold the lock for the shared resource at a time. Other threads need to wait until the current thread releases the lock. There are two types of locks in Java: Object Locks and Class Locks Synchronized keyword is used protect the piece of code being shared by multiple threads.

14 Thread Synchronization (continued…)
Synchronized can be implemented by two ways: Synchronized Methods Synchronized Blocks In the object locks, the monitor controls access to a Java object. Object locks do not work for static code. Class locks are used to control the access to synchronized static code. Synchronized method is used to control access to a method. Synchronized block is used to control access to a block of code.

15 Thread Scheduling and Priority
Thread scheduler uses scheduling algorithms to decide upon the state of the thread. Basically, there are two main algorithms: Preemptive scheduling Time sharing Every thread has an associated Thread Priority. The priority can be any integer value ranging from 1 to 10. Java provides three constants to describe the priority range: MAX_PRIORITY MIN_PRIORITY NORM_PRIORITY

16 Checked exceptions All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, it will give compilation error. Examples of Checked Exceptions :- ClassNotFoundException IllegalAccessException NoSuchFieldException EOFException etc.

17 Unchecked Exceptions Runtime Exceptions are also known as Unchecked Exceptions as the compiler do not check whether the programmer has handled them or not but it’s the duty of the programmer to handle these exceptions and provide a safe exit. These exceptions need not be included in any method’s throws list because compiler does not check to see if a method handles or throws these exceptions. Examples of Unchecked Exceptions:- ArithmeticException ArrayIndexOutOfBoundsException NullPointerException NegativeArraySizeException etc.

18 Multiple Catch Statements
If a try block is likely to raise more than one type of exceptions, then multiple catch blocks can be defined as follows: try { // statements } catch( Exception-Type1 e) { // statements to process exception 1 .. catch( Exception-TypeN e) // statements to process exception N

19 finally block Java supports definition of another block called finally that be used to handle any exception that is not caught by any of the previous statements. It may be added immediately after the try block or after the last catch block: try { // statements } catch( Exception-Type1 e) { // statements to process exception 1 .. finally { …. When a finally is defined, it is executed regardless of whether or not an exception is thrown. Therefore, it is also used to perform certain house keeping operations such as closing files and releasing system resources.

20 Catching and Propagating Exceptions
Exceptions raised in try block can be caught and then they can be thrown again/propagated after performing some operations. This can be done by using the keyword “throw” as follows: throw exception-object; OR throw new Throwable_Subclass;

21 With Exception Handling - Example 4
class WithExceptionCatchThrow{ public static void main(String[] args){ int a,b; float r; a = 7; b = 0; try{ r = a/b; System.out.println(“Result is “ + r); } catch(ArithmeticException e){ System.out.println(“ B is zero); throw e; System.out.println(“Program is complete”); Program Does Not reach here when exception occurs

22 With Exception Handling - Example 5
class WithExceptionCatchThrowFinally{ public static void main(String[] args){ int a,b; float r; a = 7; b = 0; try{ r = a/b; System.out.println(“Result is “ + r); } catch(ArithmeticException e){ System.out.println(“ B is zero); throw e; finally{ System.out.println(“Program is complete”); Program reaches here

23 User-Defined Exceptions
Problem Statement : Consider the example of the Circle class Circle class had the following constructor public Circle(double centreX, double centreY, double radius){ x = centreX; y = centreY; r = radius; } How would we ensure that the radius is not zero or negative? Circle

24 Defining your own exceptions
import java.lang.Exception; class InvalidRadiusException extends Exception { private double r; public InvalidRadiusException(double radius){ r = radius; } public void printError(){ System.out.println("Radius [" + r + "] is not valid");

25 Throwing the exception
class Circle { double x, y, r; public Circle (double centreX, double centreY, double radius ) throws InvalidRadiusException { if (r <= 0 ) { throw new InvalidRadiusException(radius); } else { x = centreX ; y = centreY; r = radius;

26 Catching the exception
class CircleTest { public static void main(String[] args){ try{ Circle c1 = new Circle(10, 10, -1); System.out.println("Circle created"); } catch(InvalidRadiusException e) { e.printError();

27 User-Defined Exceptions in standard format
class MyException extends Exception { MyException(String message) super(message); // pass to superclass if parameter is not handled by used defined exception } class TestMyException { try { .. throw new MyException(“This is error message”); catch(MyException e) System.out.println(“Message is: “+e.getMessage()); Get Message is a method defined in a standard Exception class.

28 Hackerrank Exercises


Download ppt "Multithreading in Java"

Similar presentations


Ads by Google