Sie sind auf Seite 1von 19

Multithreading introduction

Creating threads
Process and Threads
Thread life cycle
Thread priorities
Synchronizing threads

Multitasking

Multitasking:
refers to a computer's ability to perform multiple jobs concurrently
Multitasking is divided into two types:

Process-based: Here two or more programs runs concurrently. You


can run Windows calculator and a Text editor (Notepad) at the same
time.

Thread-based: A single program can perform two or more tasks


simultaneously. For example, text editor can print while formatting is
being done.
more than one program are running concurrently

What is Thread?

A thread is a single sequential flow of control


within a program.
Thread does not have its own address space but
uses the memory and other resources of the
process in which it executes.
There may be several threads in one process
The Java Virtual Machine (JVM) manages these
and schedules them for execution.

What is Multithreading ?

A thread is a single sequence of execution


within a program
refers to multiple threads of control within a
single program
each program can run multiple threads of
control within it, e.g., Web Browser

Multithreading enables programmers to do multiple


things at one time.
For ex, we can send tasks such as printing into the
background and continue to perform some other
task in the foreground.

Concurrency vs. Parallelism


CPU

CPU1

CPU2

Threads and Processes


CPU

main
run
Process 1

Process 2

Process 3

GC

Process 4

Processes & Threads

Single and
Multithreaded
Processes

THREADS

4: Threads

11

Why Multithreading?
When multiple events/actions need to
occur at the same time
Examples:
Download 10 pages.
Single-threaded program: sequentially
Multithreaded: all at the same time
save time
Download data from the network and
respond to mouse at the same time

A Single Threaded Program


class ABC
{
.
.....
.
.....
.
.....
}

Beginning

Singlethreaded
Body of
Execution
End

A Multithreaded Program
Main Method
Module

start

start

switching

start

switching

Creating Threads
:: Extending the thread class
Threads

are implemented as objects that contains a method called run()


class MyThread extends Thread
{
public void run()
{
EXAMPLE
// thread body of execution
ThreadEx1 .java
}
}
Create a thread:
MyThread thr1 = new MyThread();
Start

Execution of threads:
thr1.start();

Create

and Execute:
new MyThread().start();

Creating Threads
:: Extending the thread class : example

class MyThread extends Thread {


// the thread
public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx1 {
// a program that utilizes the thread
public static void main(String [] args ) {
MyThread t = new MyThread();
// due to extending the Thread class (above)
// I can call start(), and this will call
// run(). start() is a method in class Thread.
t.start();
} // end main()
}
// end class ThreadEx1
16

Creating Threads

:: Implementing the runnable interface

class MyThread implements Runnable


{
.....
public void run()
{
// thread body of execution
}
}
Creating Object:
MyThread myObject = new MyThread();
Creating

Thread Object:
Thread thr1 = new Thread( myObject );

Start

Execution:
thr1.start();
17

EXAMPLE
ThreadEx2.java

Creating Threads
:: Implementing the runnable interface : example

class MyThread implements Runnable {


public void run() {
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx2 {
public static void main(String [] args ) {
MyThread myObject = new MyThread();
Thread thr1 = new Thread( myObject );
thr1.start();
}

} // end main()
// end class ThreadEx2

MultithreadedEXAM
PLE
ThreadTest.java