25
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.

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

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

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{

…. }