1 Java - Threads A thread is an individual flow of control within a larger program. A program which is running more than one thread is said to be multithreaded.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Java Language Quick-Reference Guide B. Oracle10g: Java Programming B - 2 Console Output Java applications and applets can output simple messages to the.
Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
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.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
1 More on Threads b b A section of code executed independently of other threads written within a single program. b b Java threads can access global data;
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
1 Java - Threads b b A thread is an individual flow of control within a larger program. b b A program which is running more than one thread is said to.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
1 Web Based Programming Section 6 James King 12 August 2003.
Java and C++, The Difference An introduction Unit - 00.
1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other Error Handling Techniques 14.4The Basics.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Object Oriented Programming
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Chapter 12: Exception Handling
1 CSC241: Object Oriented Programming Lecture No 27.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Slides Credit Umair Javed LUMS Web Application Development.
Computer Science [3] Java Programming II - Laboratory Course Lab 3-1: Creating and Using Interfaces Exception Faculty of Engineering & IT Software Engineering.
Java Programming: Guided Learning with Early Objects
VB.Net - Exceptions Copyright © Martin Schray
Introduction to Exception Handling and Defensive Programming.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Chapter 14 – Exception Handling
Lecture 11 Objectives Learn what an exception is.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

1 Java - Threads A thread is an individual flow of control within a larger program. A program which is running more than one thread is said to be multithreaded. You can think of threads as the ability of your program to do multiple independent actions at the same time where each action runs in its own thread. Each thread can be controlled (started, stopped, prioritised) individually.

2 Threads Unlike separate programs, threads within the same class share data members making it easy for them to work co-operatively.

3 No Threads import java.awt.Graphics; import java.util.Date; public class Clock extends java.applet.Applet

4 No Threads public void start( ) { while ( true ) { repaint( ); try {clockThread.sleep( 1000 );} catch ( Exception e ) {} }}

5 No Threads public void paint( Graphics g ) { Date now = new Date( ); g.drawString(now.getHours( ) + ":" + now.getMinutes( )+ ":" + now.getSeconds( ), 5,10); }

6 Threads import java.awt.Graphics; import java.util.Date; public class Clock extends java.applet.Applet implements Runnable { Thread clockThread;

7 Threads public void start( ) { if ( clockThread == null ) { clockThread = new Thread ( this, "Clock" ); clockThread.start( ); }

8 Threads public void run( ) { while ( true ) { repaint( ); try {clockThread.sleep( 1000 );} catch ( Exception e ) {} }}

9 Threads public void stop( ) { clockThread.stop( ); clockThread = null; }

10 Threads public void paint( Graphics g ) { Date now = new Date( ); g.drawString(now.getHours( ) + ":" + now.getMinutes( )+ ":" + now.getSeconds( ), 5,10); }

11 Threads public void stop( ) { clockThread.stop( ); clockThread = null; }

12 More on Threads A section of code executed independently of other threads written within a single program. Java threads can access global data; local variables are private to each thread. Multithreading: Concurrency.

13 Implementing Threads i) The interface Runnable specifies an abstract method run() which typically will be overridden in a class implementing this interface. public test implements Runnable{ public void run(){ //body of intended thread here }

14 Implementing Threads Test is not a thread instance, so need code such as: test testing = new test(); Thread t = new Thread(testing); t.start; // to start thread executing call to start will automatically invoke run().

15 Implementing Threads ii) use: class test extends Thread then can create instance of test by writing test eg = new test(); eg.start();

16 Exception Handling Mechanisms similar to that used in C++ Some common Exceptions: –out-of bounds array subscript –arithmetic overflow (number to large to be represented) –division by zero –invalid method parameters –memory exhaustion

17 Exception Handling Java exception handling enables a program to catch –all types of exceptions, or –all exceptions of a certain type, or –all exceptions of related types Exception handling is a recovery mechanism (recovery from malfunctioning)

18 Exception Handling Java has a class called Exception. Exception subclasses can be derived from this class. When a method detects an error and the method is unable to deal with it, it throws an exception. This exception is caught only if an exception handler is present.

19 Exception Hadling try block - used to enclose a code that may generate an exception catch block - specifies the type of code to be caught and contains an exception handler. finally block (optional) - provides code that always executes regardless of whether or not exception occurs (use when there are no catch blocks)

20 Exception Handling throws - clause to specify the exceptions a method throws.

21 Exception Handling - example public class DivideByZeroException extends ArithmeticException { public DivideByZeroException() { super(“Attempted to divide by zero”); //explicit call to superclass constructor }

22 Exception Handling … try { //code that may generate divide by zero excp. } catch (DivideByZeroException e) { showStatus(e.toString()); } ….

23 Sockets Java offers socket-based-communications for network programming An application can read from/write to a socket (just like file input output) Java provides (java.net package) –stream sockets (for a process to establish a connection to another process - using TCP) –datagram sockets (to transmit individual packets - UDP is used).

24 Interfaces An interface is a special type of class that is only useful in conjunction with the implements statement An interface extends the capabilities of a class. public class aClass extends Applet implements Runnable, ActionListener { ……..}

25 Interfaces Interface ImageUtilities { void darken(); void lighten(); void flip(int degrees); } class Test implements ImageUtilities{ …. }