0% found this document useful (0 votes)
3 views15 pages

Understanding Java Threads and Synchronization

This document discusses threads in Java. It defines a thread as a sequential flow of control within a program and notes that threads allow for simultaneous execution within a program's memory space. It explains how to create new threads by extending the Thread class or implementing Runnable and how to start threads. The document also covers thread states like runnable and not runnable, stopping threads, user and daemon threads, and methods for thread management like sleep, yield, priority, join, wait, notify, and notifyAll.

Uploaded by

pavan
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)
3 views15 pages

Understanding Java Threads and Synchronization

This document discusses threads in Java. It defines a thread as a sequential flow of control within a program and notes that threads allow for simultaneous execution within a program's memory space. It explains how to create new threads by extending the Thread class or implementing Runnable and how to start threads. The document also covers thread states like runnable and not runnable, stopping threads, user and daemon threads, and methods for thread management like sleep, yield, priority, join, wait, notify, and notifyAll.

Uploaded by

pavan
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

Special Topics: Java

Cup 9: Threads

Dr. Tim Margush


Department of Mathematics and Computer Science
The University of Akron

Whats a Thread?
Thread of Control
A sequential flow of control within a
program

Threads are distinct execution


sequences occurring simultaneously
within one programs memory space
Allows efficient usage of single or
multiple processors
Jul 7, 2016

Tim Margush, Univers

9-2

Obtaining a New Thread


Define a class that
extends
[Link]
overrides the run()
method

Instantiate an
object of that class
this creates a new
thread

Jul 7, 2016

Define a class that


implements
Runnable
implements the
run() method

Instantiate an
object of that class
this creates a new
thread

Tim Margush, Univers

9-3

Starting a Thread
start() //duh!
before starting, a thread does not use
any system resources
start schedules the thread for execution
(it is in the runnable state)
the threads run() method is what gets
executed

the thread may yield to other threads,


but continues to execute when possible
Jul 7, 2016

Tim Margush, Univers

9-4

Not Runnable?
A Thread becomes Not Runnable if
its sleep method is called
becomes runnable after the specified
time period has elapsed

it calls a wait method


becomes runnable when notified that
the wait condition has been met

it is blocked on I/O
becomes runnable when the IO finishes

Jul 7, 2016

Tim Margush, Univers

9-5

Stop Thread! Stop!


A Thread stops (becomes dead)
when its run method terminates
A Thread has a stop() method that
can also be used to terminate it
throws a ThreadDeath error which
ultimately causes the termination
do not catch the error or the Thread
will not die (unless you re-throw it)
Jul 7, 2016

Tim Margush, Univers

9-6

User and Daemon


Threads
Daemon Thread
a subordinate
thread
an application will
halt when only
Daemon Threads
are left running
setDaemon(true);
call before starting
the Thread

Jul 7, 2016

User Thread
independent from
the creating
Thread
should be designed
with some stopping
criterion
Threads created from
Daemon threads are
Daemons by default

Tim Margush, Univers

9-7

Let Sleeping Threads


Lie
Thread method: sleep(long millisec);
Suspends Thread execution for specified
time - allowing other Threads to run
May throw an InterruptedException
caused by calling the Threads interrupt()
method
this wakes up a sleeping thread
the exception should be caught

sleep(long millisecs, long nanosecs);


Jul 7, 2016

Tim Margush, Univers

9-8

Yielding to Another
Thread
Thread method: yield();
allows waiting Threads a chance to
execute
allows current Thread to continue if
other Threads are not able to run

Jul 7, 2016

Tim Margush, Univers

9-9

Scheduling Conflicts
Thread method: setPriority(int);
higher priority Threads will execute
first (if several Threads are ready to run)
low priority Threads will be
preempted if a higher priority Thread
becomes runnable
equal priority Threads may share
processor resources (time-slice)
system dependent

Jul 7, 2016

Tim Margush, Univers

9-10

Joining Threads
[Link]()
suspends the current Thread until
the named Thread dies
you may specify a maximum time to
wait
InterruptedException may occur

Jul 7, 2016

Tim Margush, Univers

9-11

Thread Relatives
Unsynchronized Threads
should not use the same resources
no need to communicate between
Threads

Synchronized Threads
must access shared resources, probably
not at the same time
may need to communicate with each
other
Jul 7, 2016

Tim Margush, Univers

9-12

Synchronization
synchronized method_a(){...
synchronized method_b(){...
Two Threads cannot be executing in these
methods on the same object at the same time
different objects - OK

If methods are not synchronized, different


Threads can run in them at the same time
(even within a single method and even if
a synchronized method is executing)
Jul 7, 2016

Tim Margush, Univers

9-13

InterThread
Communication
[Link]()
suspends the current Thread that is
synchronized on this object
You may specify a maximum wait
time (in milliseconds)
releases the Thread's lock
the current Thread must have the lock on
the object, or this will fail

InterruptedException is possible
Jul 7, 2016

Tim Margush, Univers

9-14

Notifying Waiting
Threads
[Link]()
[Link]()
Places one (or all) Threads waiting for a
lock on this object in the runnable state
This may occur automatically if the
maximum wait time has elapsed
The current Thread must have a lock
on this object
Jul 7, 2016

Tim Margush, Univers

9-15

You might also like