Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads and concurrency / Safety

Similar presentations


Presentation on theme: "Threads and concurrency / Safety"— Presentation transcript:

1 Threads and concurrency / Safety
SPL – PS6 Threads and concurrency / Safety

2 Overview Threads Threading in Java Concurrency and safety
Callback functions The singleton

3 Threads Up until now, all the programs we’ve seen were sequential.
Modern operating systems allow the execution of several processes at the same time. If the tasks we need to perform are related, we can put them in the same process, this is achieved using Threads. Threads running in the same process share resources (e.g memory, code and files)

4 Using threads In order to run several tasks in parallel, two steps should be taken: Define the tasks Create threads and assign each task to a thread Defining a thread is done in Java by implementing the runnable interface, which has a single method void run().

5 Using threads (cont) Using Java’s Thread class.
The constructor of the Thread class accepts a Runnable object. Calling start() first allocates resources for the thread, and then calls the run() function of the thread.

6 Java’s executor service
An executor is a service that internally manages threads, and allows you to assign tasks for them. There are many types of executors, with different policies of managing their internal thread.

7 Concurrency and safety
Safety problems arise when multiple threads access the same resource. A shared resource is every object which is visible to more than one thread. That includes, global objects, class members, but not local function variables.

8 The Counter class The output of the program is not deterministic.
Why? Let’s look on the increment method: The second line of the code involves three basic operations: Read the value of count Increment that value by 1 Write the new value to count

9 Dangers of threads As we’ve seen, running things concurrently can be dangerous. Two tasks can interfere with one another, leading to unexpected results. Let’s observe two more cases in which a multi-threaded execution could cause problems.

10 Avoiding safety problems
There are a few design approaches for avoiding safety problems. We will go over a few approaches in this practical session

11 Thread confinement If a resource is accessed only by one thread, no synchronization is needed. You can confine certain variables to just one thread.

12 Immutability An immutable object’s state cannot be changed after construction. It can be safely used by many threads as a read only object. For example, the String, and Integer classes in Java are immutable. The final keyword in Java is similar to the const keyword in C/C++. Final fields can’t be modified, but objects they refer to can be modified if they are mutable.

13 Immutability (cont) A class will be immutable if the following are true: All of it’s fields are final The class is declared final The this reference is not allowed to escape during construction Any fields that contain references to immutable objects: Are private Are never returned or exposed to the caller Are the only reference to the object they refer to Do not change the state of the referenced object after construction.

14 Immutability example

15 Callback functions A callback function is a function which is passed to another function as a parameter. We can simulate this behavior in Java using interfaces. Anonymous classes and lambdas can capture variables, with some restrictions: An anonymous class has access to members of it’s enclosing class. An anonymous class cannot access variables in it’s enclosing scope that are not declared as final or effectively final. A declaration of a variable in an anonymous class shadows any other declaration in it’s enclosing scope.

16 The singleton In some cases, you’ll want to have exactly one instance of a class in your program. Those types of objects are accessed throughout the program, and therefore require a global point of access.

17 The singleton (cont) This naïve implementation of the singleton works well in a single threaded environment, but not in a multi-threaded one.


Download ppt "Threads and concurrency / Safety"

Similar presentations


Ads by Google