Multithreading in Java

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Multithreading in JAVA
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Exceptions: When things go wrong
Java Multithreading.
Chapter 10 – Exception Handling
Multithreading.
Java Programming Language
Multithreaded Programming in Java
Introduction to Exceptions in Java
Introduction to Exceptions in Java
Exception Handling Visit for more Learning Resources.
Handling Exceptions.
Exception Handling Chapter 9.
ATS Application Programming: Java Programming
Exceptions & exception handling
EXCEPTION HANDLING OR ERROR HANDLING.
Abdulmotaleb El Saddik University of Ottawa
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling Chapter 9 Edited by JJ.
Multithreading.
Java Based Techhnology
Multithreading.
Exception Handling in Java
CSE 143 Java Exceptions 1/18/2019.
Managing Errors and Exceptions
Lecture 11 Objectives Learn what an exception is.
Multithreading in java.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tutorial Exceptions Handling.
Threads and Multithreading
Exception Handling Contents
Tutorial MutliThreading.
Java Basics Exception Handling.
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreading in Java

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

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.

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

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.

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

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.

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

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.

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

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.

4.3 Thread States: Life Cycle of a Thread

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.

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.

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

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.

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.

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

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.

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;

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

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

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

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");

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;

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

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.

Hackerrank Exercises https://www.hackerrank.com/domains/java/handling-exceptions/difficulty/all/page/1