Threads and concurrency / Safety

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Road Map Introduction to object oriented programming. Classes
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Understanding class definitions Looking inside classes.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
!!! Global Variables!!! are EVIL SSimply because you just write a school boy/gal?
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Internet Software Development Controlling Threads Paul J Krause.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Java Thread and Memory Model
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
System Programming Practical Session 4: Concurrency / Safety.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
System Programming Practical Session 4: Concurrency / Safety.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Test 2 Review Outline.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Programming with ANSI C ++
Inheritance and Polymorphism
Chapter 19 Java Never Ends
Lecture 8 Thread Safety.
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Lecture 11 C Parameters Richard Gesick.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Group Status Project Status.
Interfaces.
Implementing Classes Chapter 3.
Dr. Mustafa Cem Kasapbaşı
Final Jim Brucker.
Java Concurrency.
Java Concurrency.
Lecture 8 Thread Safety.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
„Lambda expressions, Optional”
CMSC 202 Threads.
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
More concurrency issues
Introduction to Classes and Objects
Presentation transcript:

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

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

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)

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().

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.

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.

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.

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

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.

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

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

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.

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.

Immutability example

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.

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.

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