CSC 143 Threads CSC 143 1

advertisement

CSC 143

Threads

CSC 143 1

Introducing Threads

A thread is a flow of control within a program

A piece of code that runs on its own. The execution environment is the same as for the rest of the program. The thread can share data with other parts (=other threads) of the program.

A thread executes concurrently with the other threads of the program.

 e.g. when painting (or repainting), the method paint is executed by a thread.

Key issue: synchronization

If the threads run concurrently and share data, what happens when one thread uses data modified at the same time by another thread?

CSC 143 2

The Thread class

 in the package java.lang

Define a thread by extending the Thread class

 override the run method to specify what the thread will do public class MyThread extends Thread{ public void run(){

/* code for thread to run when it gets control */ }

// more code

}

 to make a thread, write

MyThread t = new MyThread(); t.start();

CSC 143 3

Life of a thread

When start is called, the thread t becomes ready to be executed by the processor.

When another thread gets a turn, t will stop executing its run method. It will start again from where it left off, when it gets another turn

The scheduling of the threads is the responsibility of the OS

However, we can temporarily stop the execution, e.g. using the methods sleep and wait (see the examples to come)

CSC 143 4

Example

Create 2 threads that write their names and sleep

 see the full code on the class web site:

ThreadName.java

Can you predict the order of execution?

CSC 143 5

Using the Runnable interface

If our class already extends another class, how do we make it a thread?

public class D extends B

Use the Runnable interface and implement the run method public class D extends B implements Runnable{

//Thread to host our run method private Thread t;

// implement run public void run(){ /*code*/ }

// to start the thread public D(){ t = new Thread( this ); t.start(); }}

How would you program the example

ThreadName using Runnable?

CSC 143 6

Concurrent threads

When 2 threads running independently can modify the same data, they need to cooperate.

If not, the state of the data is unpredictable

See an example : BadConcurrency.java on the class web site.

To avoid the problem, synchronize the two threads,e.g. in BadConcurrency.java write: synchronized (data){ data[0]=val; data[1]=val; checkData();}

// The thread must have an exclusive

// access to data before entering the

CSC 143 7 // synchronized block

synchronization

Use the keyword synchronized

What can be synchronized?

A block of Java code (as in the previous example). synchronized (myObject){ /*block*/ }

The thread needs a lock on myObject before entering the block.

An instance method, e.g.

public synchronized void update()

{ /*code*/ }

 a thread can execute update only if it has a lock on the class object ( this ).

CSC 143 8

wait method

 available for any object since it is part of the

Object class

 inside of a synchronized block, a thread can give up its hold on a lock by calling wait. Then, another thread can take the lock.

When the first thread reacquires its lock, it starts from where it left off.

Reacquisition is not assured. The thread must wake up. This is done if another thread calls notify() or notifyAll(). Then the thread is requeued and competes for the lock with the other threads.

CSC 143 9

Example

A Sender sends items to a Receiver via a

Buffer

Both the Sender and the Receiver can modify the Buffer content.

The access to the Buffer must be synchronized if Sender and Receiver are two independent threads.

See TestSenderReceiver.java (and other files) on the class web site.

CSC 143 10

deadlock

When threads wait for locks that can't be freed.

 e.g. in the Sender-Receiver example, there is deadlock if

The Sender sends only to an empty buffer

The Receiver takes only from a full buffer

CSC 143 11

Animation

Animation is better achieved with threads

Without an animation thread, one part of the program can hold up the animation part.

CSC 143 12

Download