Java Features - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Java Features

Description:

... executed in parallel with rest of program after the thread is started ... Ensure threads are ready to interact. Imagine a waiter writing orders on a blackboard ... – PowerPoint PPT presentation

Number of Views:354
Avg rating:3.0/5.0
Slides: 20
Provided by: julie57
Category:
Tags: features | java | threads

less

Transcript and Presenter's Notes

Title: Java Features


1
Java Features
  • If you understand this, you will be able to
  • Read code containing exceptions
  • Important Exceptions make programs more robust
  • Java makes you handle exceptions
  • Outline the benefits problems of thread use
  • Important communication is usually concurrent

2
Key Features of Java
  • Exceptions
  • Separate normal code and error-handling code
  • Handlers respond to exceptional occurrences
  • Uniformly handles all sorts of error
  • Concurrency built into the language
  • Several methods executing at the same time
  • Not an operating-system dependent add-on

3
Exceptions
  • Respond to Illegal situations from
  • Hardware / Operating System
  • Language (generated by run-time system)
  • Software (explicit use of throw statement)
  • An exception object
  • Contains information about the situation
  • A handler
  • Catches the exception
  • Performs appropriate programming

4
Examples of Exceptions
  • Divide by zero
  • Reading from a file, but disk corrupt
  • Writing, but disk full
  • Out of memory
  • Data communication link failure

5
Why use exceptions
  • Java goal portability and robustness
  • Alternative to
  • Automatic program termination
  • Undefined (unpredictable) response
  • Returning funny values - often ignored 

6
Handling an Exception
  • Handlers are catch clauses
  • try
  • // statements that may throw an exception
  • catch (ExceptionType e)
  • ... // Catches only exceptions of this type

Information about the exception, e.g. an error
message
7
Steps
  • Exception object thrown
  • Abort blocks and look for a handler for that
    class of exception object.
  • If no handler is found, abort program.
  • Otherwise execute the catch block.
  • Carry on with the following statement.

8
Exception Example
  • class MyException extends Exception
  • Define a new exception class
  • To hold information about a particular problem
  • e.g. noticing that a user has left a multi-player
    game
  • Can add properties relevant to this situation
  • Can catch just this class of exception

9
Typical use of exceptions
  • try
  • link new Link(OpponentDetails)
  • // play the game
  • average score / numTries
  • link.send("Average "average)
  • catch(DivideByZero ex)
  • // handle divide by zero
  • catch(CommsError ex)
  • // handle communications problem

Normal code
Handler
10
Deallocating Resources
  • Must return resources even if an exception occurs
  • But the block is terminated skipping statements
  • A finally clause is always executed
  • Allowing resources to be deallocated.
  • // Allocate resource
  • try
  • // code that could generate an exception
  • finally
  • // return the resource

11
De-allocating Resources
  • try
  • link new Link(OpponentDetails)
  • try
  • // play the game
  • finally
  • link.close()
  • catch(DivideByZero ex)
  • catch(CommsError ex)

We can be sure that the link will be closed
Unless someone nukes the computer
12
Compile-Time Checking
  • Compiler checks exception handlers
  • A method must either
  • handle all checked exceptions or
  • name them in the header with a throws clause
  • (part of contract with user)
  • Some exceptions arent checked
  • Very severe or very common
  • Why these?
  • Ensures exceptions handled why?

13
Threads
  • Single thread of execution
  • Code executes one statement at a time
  • Multiple Threads
  • Independently execute code at the same time
  • using data in shared main memory.
  • The Java Virtual Machine supports many threads
  • Implemented by
  • multiple processors,
  • time-slicing a single hardware processor
  • Treat as if each has its own processor

14
Why Threads
  • Allow parallel processing
  • Multiple tasks of varying duration
  • Logical division of tasks
  • Break problem into modules
  • Receive task, Send task
  • Keep user interface responsive
  • Other threads deal with data communications

15
Declaring a Thread
  • class MouseChase implements runnable
  • void run()
  • while (true)
  • // move to next position
  • repaint()
  • sleep(100)

This code will be executed in parallel with rest
of program after the thread is started
A Thread uses a class with a run() method
16
Starting a thread
  • Create a runnable object.
  • MouseChase myMC new MouseChase()
  • Create a thread using the runnable object
  • Thread myThread new Thread(myMC)
  • Start the thread
  • myThread.start()
  • The thread now has a life of its own.
  • What if start() is replace by run()?

17
Concurrency Issues
  • Synchronisation
  • Ensure threads are ready to interact
  • Imagine a waiter writing orders on a blackboard
  • The chef copies them into his notebook
  • Communication
  • Exchanging data safely
  • Requires synchronisation
  • Deadlocks
  • Cycle of threads holding resources others need

18
Problems
  • Using a bank account example, explain
  • How deadlock can occur transferring money
  • Why synchronisation is needed

19
Summary
  • Exceptions
  • Make program more robust
  • Compile time checking enforces use
  • Threads
  • Useful to keep user interface responsive
  • Increase
  • Likelihood of errors
  • Complexity of debugging
Write a Comment
User Comments (0)
About PowerShow.com