0% found this document useful (0 votes)
11 views14 pages

Multithreading I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Multithreading I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Multithreading

Multithreaded Programming
• Unlike many other computer languages, Java provides built-in
support for multithreaded programming.

• A multithreaded program contains 2 or more parts that can run


concurrently. Each part of such a program is called a thread,
and each thread defines a separate path of execution.

• There are 2 distinct types of multitasking: process-based and


thread-based.

• A process is, in essence, a program that is executing. Thus,


process-based multitasking allows your computer to run the
Java compiler at the same time that you are using a text editor.

2
Multithreaded Programming
• In a thread-based multitasking, a single program can perform 2
or more tasks simultaneously.

• For instance, a text editor can format text at the same time that it
is printing, as long as these two actions are being performed by
two separate threads.

• Multitasking threads require less overhead than multitasking


processes.

• Processes are heavyweight tasks that require their own separate


address spaces. Interprocess communication is expensive and
limited.

• Context switching from one process to another is also costly.


3
Multithreaded Programming
• Threads, on the other hand, are lightweight. They share the
same address space and cooperatively share the same
heavyweight process.

• Interthread communication is inexpensive, and context


switching from one thread to the next is low cost.

• Multithreading enables you to write very efficient programs that


make maximum use of the CPU, because idle time can be kept
to a minimum.

• In a single-threaded environment, your program has to wait for


different tasks to finish before it can proceed to the next one –
even though the CPU is sitting idle most of the time.
4
Threaded States: Life Cycle of a
Thread

Thread States
• Born State
 Thread was just created
• Ready State
 Thread’s start method invoked
 Thread can now execute
• Running State
 Thread is assigned a processor and running
• Dead State
 Thread has completed or exited
 Eventually disposed of by system
Thread States: Life Cycle of a Thread

State diagram showing the Life cycle of a thread


born

start

read y
q uantum
exp iratio n d isp atch

I/ O
yield (a ssig n a
Al l

co
inte rrup t p roce ssor)
ot i y

mp
t if
fy

running

let
no

ion
issu
eI
n

/O
or

co
it re q
wa ue

m
p
st
ee

ple
sl

te
wa iting slee ping de ad blocked

slee p inte rval


expires
Thread Priorities
• Java assigns to each thread apriority that determines how that
thread should be treated with respect to the others.

• Thread priorities ae integers that specifies the relative priority


of one thread to another.

• As an absolute value, a priority is meaningless; a higher priority


thread doesn’t run any faster than a lower-priority thread if it is
the only thread running.

• Instead, a thread’s priority is used to decide when to switch


from one running thread to the next. This is called a context
switch.

7
Synchronization

• Java uses monitors for thread synchronization

• The sychronized keyword

• Every synchronized method of an object has a monitor


• One thread inside a synchronized method at a time
• All other threads block until method finishes
• Next highest priority thread runs when method finishes

8
The Thread Class and the Runnable
Interface
• Java’s multithreading system is built upon the Thread class, its
methods, and its companion interface, Runnable.

• To create a new thread, your program will either extend Thread


or implement the Runnable interface.

• The Thread class defines several methods that help manage


threads.
Method Meaning
getName Obtain a thread’s name
getPriority Obtain a thread’s priority
isAlive Determine if a thread is still running
join Wait for a thread to terminate
run Entry point for the thread
sleep Suspend a thread for a period of time
9
start Start a thread by calling its run
The Main Thread
• When a Java program starts up, one thread begins
running immediately. This is usually called the main
thread of your program, because it is the one that is
executed when your program begins.

• The main thread is important for reasons:


 It is the thread from which other “child” threads
will be spawned.
 Often, it must be the last thread to finish execution
because it performs various shutdown actions.
10
The Main Thread
• Although the main thread is created automatically when your
program is started, it can be controlled through a Thread
object.

• To do so, you must obtain a reference to it by calling the


method currentThread(), which is a public static member of
Thread. Its general form is:

static Thread currentThread()

• This method returns a reference to the thread in which it is


called. Once you have a reference to the main thread, you can
control it just like any other thread.

11
Creating a Thread

In the most general sense, you create a thread by instantiating an


object of type Thread. Java defines 2 ways in which this can be
accomplished:

•You can implement the Runnable interface.

•You can extend the Thread class itself.

12
Implementing Runnable
• The easiest way to create a thread is to create a class that implements
the Runnable interface. Runnable abstracts a unit of executable
code. You can construct a thread on any object that implements
Runnable. To implement Runnable, a class need only implement a
single method called run(), which is declared like this:

public void run();

• Inside run(), you will define the code that constitutes the new
thread. It is important to understand that run() can call other
methods, use other classes, and declare variables, just like the main
thread can. The only difference is that run() establishes the entry
point for another, concurrent thread of execution within your
program. This thread will end when run() returns.

13
Choosing an Approach

At this point, you might be wondering why Java has 2 ways to create
child threads, and which approach is better. The answers to these
questions turn on the same point. The Thread class defines several
methods that can be overridden by a derived class. Of these methods,
the only one that must be overridden is run(). This is of course, the
same method required when you implement Runnable. Many Java
programmers feel that classes should be extended only when they are
being enhanced or modified in some way. So, if you will not be
overriding any of Thread’s other methods, it is probably best simply to
implement Runnable. This is up to you, of course. However,
throughout the rest of the programs, we will create threads by using
classes that implement Runnable.

14

You might also like