Distributed System Lab2A
Distributed System Lab2A
Department of SE and IT
4th Level
Second Lab : Synchronization
2022 – 2023
Prepared by
Eng. Salah Alssayani
Synchronization
Developing multithreaded applications is much easier when threads don’t interact, typically via shared
variables. When interaction occurs, various problems can arise that make an application thread-unsafe
(incorrect in a multithreaded context). In this LAB II, you’ll learn about these problems and also learn
how to overcome them through the correct use of Java’s synchronization-oriented language features.
Race Conditions
A race condition occurs when the correctness of a computation depends on the relative timing or
interleaving of multiple threads by the scheduler.
There is no problem with this code fragment in a single-threaded context, and there is no problem in a
multithreaded context when data are local variables.
Data Races
A race condition is often confused with a data race in which two or more threads (in a single
application) access the same memory location concurrently, at least one of the accesses is for writing,
and these threads don’t coordinate their accesses to that memory. When these conditions hold, access
order is non-deterministic. Different results may be generated from run to run, depending on that order.
Cached Variables
To boost performance, the compiler, the Java virtual machine (JVM), and the operating system can
collaborate to cache a variable in a register or a processor-local cache, rather than rely on main
memory. Each thread has its own copy of the variable. When one thread writes to this variable, it’s
writing to its copy; other threads are unlikely to see the update in their copies.
import java.math.BigDecimal;
/*
* Compute the value of pi to the specified number of digits after the
* decimal point. The value is computed using Machin's formula:
*
* pi/4 = 4*arctan(1/5)-arctan(1/239)
*
* and a power series expansion of arctan(x) to sufficient precision.
*/
/*
* Compute the value, in radians, of the arctangent of the inverse of
* the supplied integer to the specified number of digits after the
* decimal point. The value is computed using the power series
* expansion for the arc tangent:
*
* arctan(x) = x-(x^3)/3+(x^5)/5-(x^7)/7+(x^9)/9 ...
*/
@Override
public void run(){
synchronized(this) {
while(!stopped)
System.out.println("running");
}
}
@Override
public void run() {
while(!stopped)
System.out.println("running");
}
void stopThread(){
stopped = true;
}
}
StoppableThread thd = new StoppableThread();
thd.start();
try {
Thread.sleep(1000); // sleep for 1 second
}
catch (InterruptedException ie) {
}
thd.stopThread();
}
}
import java.util.Set;
import java.util.TreeSet;
public final class Planets {
private final Set<String> planets = new TreeSet<>();
public Planets() {
planets.add("Mercury");
planets.add("Venus");
planets.add("Earth");
planets.add("Mars");
planets.add("Jupiter");
planets.add("Saturn");
planets.add("Uranus");
planets.add("Neptune");
}
public boolean isPlanet(String planetName) {
return planets.contains(planetName);
}
}