Download presentation
Presentation is loading. Please wait.
1
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; local variables are private to each thread. b b Multithreading: Concurrency.
2
2 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 }
3
3 Implementing Threads b b 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 b b call to start will automatically invoke run().
4
4 Implementing Threads ii) use: class test extends Thread then can create instance of test by writing test eg = new test(); eg.start();
5
5 Exception Handling b Mechanisms similar to that used in C++ b Some common Exceptions: out-of bounds array subscriptout-of bounds array subscript arithmetic overflow (number to large to be represented)arithmetic overflow (number to large to be represented) division by zerodivision by zero invalid method parametersinvalid method parameters memory exhaustionmemory exhaustion
6
6 Exception Handling b Java exception handling enables a program to catch all types of exceptions, orall types of exceptions, or all exceptions of a certain type, orall exceptions of a certain type, or all exceptions of related typesall exceptions of related types b Exception handling is a recovery mechanism (recovery from malfunctioning)
7
7 Exception Handling Java has a class called Exception. b Exception subclasses can be derived from this class. b When a method detects an error and the method is unable to deal with it, it throws an exception. b This exception is caught only if an exception handler is present.
8
8 Exception Hadling b try block - used to enclose a code that may generate an exception b catch block - specifies the type of code to be caught and contains an exception handler. b finally block (optional) - provides code that always executes regardless of whether or not exception occurs (use when there are no catch blocks)
9
9 Exception Handling b throws - clause to specify the exceptions a method throws.
10
10 Exception Handling - example public class DivideByZeroException extends ArithmeticException { public DivideByZeroException() { super(“Attempted to divide by zero”); //explicit call to superclass constructor }}
11
11 Exception Handling … try { //code that may generate divide by zero excp. } catch (DivideByZeroException e) { showStatus(e.toString());}….
12
12 Sockets b Java offers socket-based-communications for network programming b An application can read from/write to a socket (just like file input output) b Java provides (java.net package) stream sockets (for a process to establish a connection to another process - using TCP)stream sockets (for a process to establish a connection to another process - using TCP) datagram sockets (to transmit individual packets - UDP is used).datagram sockets (to transmit individual packets - UDP is used).
13
13 Interfaces b An interface is a special type of class that is only useful in conjunction with the implements statement b An interface extends the capabilities of a class. public class aClass extends Applet implements Runnable, ActionListener { ……..}
14
14 Interfaces Interface ImageUtilities { void darken(); void lighten(); void flip(int degrees); } class Test implements ImageUtilities{ …. }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.