Chapter6 threads

26
Java2 An Introduction to Threads

description

computer

Transcript of Chapter6 threads

Page 1: Chapter6 threads

Java2

An Introduction to Threads

Page 2: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 2 of 26

Objectives

Define a thread Discuss Benefits of multithreading Explain how to create threads Describe Thread states Discuss Thread priorities Discuss Daemon thread

Compare Multitasking and Multithreading

Page 3: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 3 of 26

Multitasking Vs Multithreading

Operating system controls the way in which these programs run by scheduling them

Time between switching of programs is minute

Multithreading is the ability to execute different parts of a program called threads, simultaneously

Multitasking is the ability to run one or more programs concurrently

Page 4: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 4 of 26

Thread

An application can be divided into multiple tasks and each task can be assigned to a thread

Many threads executing simultaneously is termed as Multithreading

Although it may appear that the processes are running concurrently, it is not so

Smallest unit of executable code that performs a task

Page 5: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 5 of 26

Benefits of multithreading

In multitasking, processes run in their own different address space

Tasks involved in multithreading can share the same address space

Inter-process calling involves more overhead than inter-thread communication

Multithreading requires less overhead than multitasking

Page 6: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 6 of 26

Applications of thread

Displaying scrolling text patterns or images on the screen

Playing sound and displaying images simultaneously

Displaying multiple images on the screen

Page 7: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 7 of 26

Creating threads (1)

It is this thread from which child threads are created

Program is terminated when main thread stops execution

Main thread can be controlled through ‘Thread’ objects

When Java programs execute, there is always one thread running and that is the main thread

Page 8: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 8 of 26

Creating threads (2)

While using applets, Thread class cannot be extended. Therefore one has to implement the Runnable interface

Thread objects can be created in two ways: Declare a class that is a sub-class of the class

Thread defined in java.lang package class mythread extends Thread

Declare a class that implements the Runnable interface

class mythread implements Runnable

Page 9: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 9 of 26

Creating threads (3)

When start() method is invoked, a new thread of control is created

It then calls the run() method

To initiate a new thread, we use the start() method

Mythread t = new Mythread();

t.start();

Page 10: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 10 of 26

Creating threads (4) Example 1 – Creating a thread by

extending the Thread class

Output

Page 11: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 11 of 26

Creating threads (5) Example2 – Creating a thread by

implementing the Runnable interface

Output

Page 12: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 12 of 26

Thread states (1)

Ready – after a thread is created, it is in its ready state waiting for start() method to be called

Born – a newly created thread is in a born state

Page 13: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 13 of 26

Thread states (2)

Running – thread enters the running state when it starts executing

Sleeping – execution of a thread can be halted temporarily by using sleep() method. The thread becomes ready after sleep time expires

Page 14: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 14 of 26

Thread states (3)

Dead – after the run() method has finished or the threads stop() method is called

Waiting – thread is in waiting state if wait() method has been invoked

Blocked – when the thread waits for an Input/Output operation

Page 15: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 15 of 26

Different thread states

New Thread(BORN)

READY

RUNNING

DEAD

SLEEPING

WAITING

SUSPENDED

BLOCKED

Page 16: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 16 of 26

Some methods of thread class (1)

getName() – returns the name of the thread

start() – used to start a thread by calling the method run()

isAlive() – returns true if the thread is alive

Page 17: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 17 of 26

Some methods of thread class (2)

yield() – causes the currently executing thread to temporarily pause and allow other threads to execute

setName(String name) – sets the name of the thread to the name that is passed as argument

resume() – to resume the execution of a suspended thread

Page 18: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 18 of 26

Some methods of thread class (3)

isDaemon() – checks if the thread is a Daemon thread

activeCount() – returns the number of active threads

sleep() – used to suspend a thread for a certain period of time

join() – waits for the thread to die

Page 19: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 19 of 26

Conditions preventing thread execution

Put to sleep using sleep() method Suspended using suspend() method Thread is waiting because wait() method was

called Explicitly yielded using yield() method Blocked for file I/O

Thread is: Not of highest priority

Page 20: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 20 of 26

Managing threads (1)

Similarly while programming, we may have to run a thread of higher importance without stopping or suspending the current running thread

Thread priorities play an important role in such a situation

Priorities for carrying out activities changes at times eg :Planned to visit museum in the afternoon but

due to toothache, had to go to doctor

Page 21: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 21 of 26

Managing threads (2)

The default priority is NORM_PRIORITY Two methods used to change priority:

setPriority() - changes the thread’s current priority getPriority() - returns the thread’s priority

Thread priorities in Java are constants defined in the Thread class

NORM_PRIORITY – value is MAX_PRIORITY – value is MIN_PRIORITY– value is

Page 22: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 22 of 26

Daemon threads (1)

When user thread exits, JVM checks to find out if any other thread is running

If the only executing threads are Daemon threads, it exits

Two types of threads in Java: User threads – created by the user Daemon threads – threads that work in the

background providing service to other threads e.g. – the garbage collector thread

Page 23: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 23 of 26

Daemon threads (2)

Thread class has two methods to deal with Daemon threads: public void setDaemon(boolean value) : sets a

thread to be a daemon thread public void isDaemon() : checks if the given thread

is a daemon thread

We can set a thread to be a Daemon if we do not want the main program to wait until a thread ends

Page 24: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 24 of 26

Daemon threads (3)

An example

Output

Page 25: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 25 of 26

Session summary (1)

Threads can be used in two ways: Declare the class to be a sub-class of the Thread

class Declare a class that implements the Runnable

interface

When Java programs are executed, there is always one thread that is running and that is the main thread

Page 26: Chapter6 threads

eACCP 2002/Sem 1/ JPL/ Session 6/ 26 of 26

Session summary (2)

The default priority of a thread that is created is 5

The methods Thread.suspend(), Thread.resume() and Thread.stop() have been deprecated since Java2

The threads providing service to other threads are referred to as daemon threads

Each thread in a Java program is assigned a priority