More on Treads - Middlesex University

Download Report

Transcript More on Treads - Middlesex University

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.
1
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
}
}
2
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().
3
Implementing Threads
ii) use:
class test extends Thread
then can create instance of test by
writing
test eg = new test();
eg.start();
4
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
5
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)
6
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.
7
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)
8
Exception Handling
 throws
- clause to specify the exceptions a
method throws.
9
Exception Handling - example
public class DivideByZeroException
extends ArithmeticException {
public DivideByZeroException() {
super(“Attempted to divide by zero”);
//explicit call to superclass constructor
}
}
10
Exception Handling
…
try {
//code that may generate divide by zero excp.
}
catch (DivideByZeroException e) {
showStatus(e.toString());
}
11
….
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).
12
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 { ……..}
13
Interfaces
Interface ImageUtilities {
void darken();
void lighten();
void flip(int degrees);
}
class Test implements ImageUtilities{
…. }
14