












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of multithreading and synchronization in java through the lens of prime number testing and the producer-consumer problem. It covers the creation of threads using the thread class, the concept of mutual exclusion and thread synchronization, and the prevention of deadlock. The document also includes code examples and explanations of various thread states.
Typology: Study notes
1 / 52
This page cannot be seen from the preview
Don't miss anything!













































CNT 4714: Threading Part 2
Page 1
Mark Llewellyn ©
School of Electrical Engineering and Computer Science
University of Central Florida
Instructor :
Dr. Mark [email protected] 236, 407-823-2790http://www.cs.ucf.edu/courses/cnt4714/fall
CNT 4714: Threading Part 2
Page 2
Mark Llewellyn ©
Threads
-^
-^
-^
CNT 4714: Threading Part 2
Page 4
Mark Llewellyn ©
//driver class to demonstrate threaded prime number testerpublic class testPrime {public static void main (String s[]) {
//number to be tested for primality is entered as a command line argument//examples: 5557 is prime, 6841 is prime, 6842 is not primelong possPrime = Long.parseLong(s[0]);
int centuries = (int) (possPrime/100) + 1;for (int i=0; i<centuries;i++) {new testRange(i*100, possPrime).start();}
}
}
Driver Class for PrimeNumber Tester
-^
This is an example of related but unsynchronized threads. In this case thethreads are related since they are each working on a piece of the samedata, but approach it from a slightly different perspective. However, theyare unsynchronized since they do not share information.
CNT 4714: Threading Part 2
Page 5
Mark Llewellyn ©
6841 is prime
2048 and 6842 are notprime – their factorsare shown by thethread whichdiscovered the factor.
CNT 4714: Threading Part 2
Page 7
Mark Llewellyn ©
// class to simulate a steam boiler to illustrate a race condition in unsynchronized threadspublic class SteamBoiler {static int pressureGauge = 0;static final int safetyLimit = 20;public static void main(String [] args) {
pressure []psi = new pressure[10];for (int i = 0; i < 10; i++) {
psi[i] = new pressure();psi[i].start(); } //we now have 10 threads in execution to monitor the pressuretry {for (int i = 0; i < 10; i++)
psi[i].join(); //wait for the thread to finish } catch (Exception e) { } //do nothingSystem.out.println("Gauge reads " + pressureGauge + ", the safe limit is 20"); } }
Class to Simulate a Steam Boiler – Pressure Gauge
CNT 4714: Threading Part 2
Page 8
Mark Llewellyn ©
Thread Class to Read Steam Boiler Pressure Gauge andIncrease the Pressure if Within Range
//thread class to raise the pressure in the Boilerclass pressure extends Thread {void RaisePressure() {if (SteamBoiler.pressureGauge < SteamBoiler.safetyLimit-15) {
//wait briefly to simulate some calculationstry {sleep(100); } catch (Exception e) { }SteamBoiler.pressureGauge+= 15; //raise the pressure 15 psiSystem.out.println("Thread " + getName() + " finds pressure within limits -
increases pressure");}
else ; //the pressure is too high - do nothing }// end RaisePressurepublic void run() {
RaisePressure(); //this thread is to raise the pressure
} }
CNT 4714: Threading Part 2
Page 10
Mark Llewellyn ©
-^
You may remember the large North American power blackout that occurred onAugust 14, 2003.
Roughly 50 million people lost electrical power in a region
stretching from Michigan through Canada to New York City.
It took three days
to restore service to some areas.
-^
There were several factors that contributed to the blackout, but the official reporthighlights the failure of the alarm monitoring software which was written in C++by GE Energy. The software failure wrongly led operators to believe that all waswell, and precluded them from rebalancing the power load before the blackoutcascaded out of control.
-^
Because the consequences of the software failure were so severe, the bug wasanalyzed exhaustively.
The root cause was finally identified by artificially
introducing delays in the code (just like we did in the previous example).
There
were two threads that wrote to a common data structure, and through a codingerror, they could both update it simultaneously.
It was a classic race condition,
and eventually the program “lost the race”, leaving the structure in an inconsistentstate.
That in turn caused the alarm event handler to spin in an infinite loop, instead of raising the alarm.
The largest power failure in the history of the US
and Canada was caused by a race condition bug in some threaded C++ code.Java is equally vulnerable to this kind of bug.
CNT 4714: Threading Part 2
Page 11
Mark Llewellyn ©
The Therac-25 Accidents
-^
Starting in 1976, the Therac-25 treatment system, built by Atomic Energyof Canada Limited (AECL) and COR MeV of France, was used to fightcancer by providing radiation to a specific part of the body in the hope ofdestroying tumors.
-^
Six known Therac-25 accidents have been documented, all involvedmassive overdoses of radiation and three resulted in the death of thepatient, serious long-term injury and disfigurement occurred in the othercases. Patients received an estimated 17,000 to 25,000 rads to very smallbody areas.
By comparison, doses of 1000 rads can be fatal if delivered
to the whole body.
-^
Analysis determined that the primary cause of the overdoses was faultysoftware.
The software was written in assembly language and was
developed and tested by the same person.
The software included a
scheduler and concurrency in its design. When the system was first built,operators complained that it took too long to enter the treatment plan intothe computer.
As a result, the software was modified to allow operators
to quickly enter treatment data by simply pressing the Enter key when aninput value did not require changing.
CNT 4714: Threading Part 2
Page 13
Mark Llewellyn ©
Thread Synchronization
-^
-^
-^
CNT 4714: Threading Part 2
Page 14
Mark Llewellyn ©
Thread Synchronization
(cont.)
-^
The next selected thread will be based on some protocol.
The
most common of these is simply FCFS (priority-queue based).
-^
In this fashion, each thread accessing the shared objectexcludes
all
other
threads
from
accessing
the
object
simultaneously.
This is the process known as mutual
exclusion.
-^
Mutual
exclusion
allows
the
programmer
to
perform
thread
synchronization,
which
coordinates
access
to
shared objects by concurrent threads.
CNT 4714: Threading Part 2
Page 16
Mark Llewellyn ©
Synchronization Techniques
(cont.)
-^
Read/write Locks.
These are also commonly referred to as mutexes
(although
some
people
still
use
the
term
mutex
to
refer
to
a
semaphore.) A lock provides a simple ”turnstile”: only one thread ata time can be going through (executing in) a block protected by alock. Again, it is easy to build a lock from semaphores.
-^
Monitors. A monitor is a higher-level synchronization construct builtout of a lock plus a variable that keeps track of some relatedcondition, such as “the number of unconsumed bytes in the buffer”.It is easy to build monitors from read/write locks. A monitor definesseveral methods as a part of its protocol.
Two of those predefined
methods are
wait()
and
notify()
.
CNT 4714: Threading Part 2
Page 17
Mark Llewellyn ©
Types of Synchronization
-^
Mutual exclusion is used to protect certain critical sections of codefrom
being
executed
simultaneously
by
two
or
more
threads.
(Synchronization without cooperation.)
2.^
Signal-wait is used when one thread need to wait until another threadhas completed some action before continuing. (Synchronization withcooperation.)
-^
-^
CNT 4714: Threading Part 2
Page 19
Mark Llewellyn ©
-^
-^
The constructor for a
ReentrantLock
takes a boolean argument
that specifies whether the lock has a fairness policy.
If this is set to
true,
the
ReentrantLock’s
fairness
policy states that the longest-
waiting thread will acquire the lock when it is available.
If set to
false, there is no guarantee as to which waiting thread will acquire thelock when it becomes available.
-^
CNT 4714: Threading Part 2
Page 20
Mark Llewellyn ©
Condition Variables
-^
-^
-^
-^