Advanced Programming C# Lecture 13. dr inż. Małgorzata Janik

Similar documents
Advanced Programming C# Lecture 12. dr inż. Małgorzata Janik

Threading in C# Joseph Albahari

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

Systems Programming & Scripting

Object Oriented Programming. Week 10 Part 1 Threads

Exercise Session Week 8

Multiple processes can run in parallel on a single computer and multiple threads can run in parallel in a single process.

Exercise Session Week 8

Programming in.net. Microsoft Development Center Serbia programming course. Lesson 8 Parallelism and Threading in.net

Animation Part 2: MoveableShape interface & Multithreading

Threads Chate Patanothai

DAD Lab. 2 Additional C# Topics

Threads & Networking

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Java Threads. Written by John Bell for CS 342, Spring 2018

CS11 Java. Fall Lecture 7

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Contents. 6-1 Copyright (c) N. Afshartous

Lecture 8: Threads. Lisa (Ling) Liu

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Advanced Programming C# Lecture 9. dr inż. Małgorzata Janik

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Introduction to Java Threads

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Java s Implementation of Concurrency, and how to use it in our applications.

Multithreading. Multithreading. HIS CHAPTER DISCUSSES how to write multithreaded code. To do this, Another Thread

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

7. MULTITHREDED PROGRAMMING

Unit - IV Multi-Threading

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

UNIT V CONCURRENT PROGRAMMING

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

CSCD 330 Network Programming

CS 153 Design of Operating Systems Winter 2016

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

CSCD 330 Network Programming

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

CMSC 132: Object-Oriented Programming II

CS 351 Design of Large Programs Threads and Concurrency

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

CMSC 330: Organization of Programming Languages

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

COE518 Lecture Notes Week 7 (Oct 17, 2011)

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

JAVA. Lab 12 & 13: Multithreading

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

Multithreading Pearson Education, Inc. All rights reserved.

Concurrent Computing CSCI 201 Principles of Software Development

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

Multithreading. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

JAVA - MULTITHREADING

Advanced Programming C# Lecture 10. dr inż. Małgorzata Janik

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

Advanced Concepts of Programming

Multithreading and Interactive Programs

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Game Engineering: 2D

Advanced Programming C# Lecture 12. dr inż. Małgorzata Janik

Java Programming Lecture 23

CS 159: Parallel Processing

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Java Threads. Introduction to Java Threads

Multithread Computing

Concurrency. Fundamentals of Computer Science

CS360 Lecture 12 Multithreading

CMSC 132: Object-Oriented Programming II. Threads in Java

Java Threads. COMP 585 Noteset #2 1

Performance Throughput Utilization of system resources

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

Multithreaded Programming

Synchronization synchronization.

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Chapter 13 Working with Threads

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Software Practice 3 Today s lecture Today s Task

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Lecture 21: Concurrency in Other Environments (Part 2)

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Chapter 32 Multithreading and Parallel Programming

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Last Class: Deadlocks. Today

Multithreaded Programming

ECE Fall 2015, First Exam

Programming Language Concepts: Lecture 11

Module - 4 Multi-Threaded Programming

Managed Threading 1 Managed Threading Basics 3 Threads and Threading 5 Synchronizing Data for Multithreading 8 Foreground and Background Threads 11

THREADS AND CONCURRENCY

Transcription:

Advanced Programming C# Lecture 13 dr inż. Małgorzata Janik majanik@if.pw.edu.pl Winter Semester 2017/2018

Project

C# 2017/2018, Lecture 13 3 / 24 Project part III Final Date: 22.01.2018 (next week!) Today some previews (hopefully) Prepare the presentation and/or documentation that describes the project To be shown to the whole group Instead of using slides you can show your program All projects & documentation should be sent to: majanik@if.pw.edu.pl. Project can (should) be shared via git repository.

Multithreading

Threading in C# C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads. A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the main thread), and is made multithreaded by creating additional threads. C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 5 / 24

Threading: basics class ThreadTest static void Main() Thread t = new Thread (WriteY); // Kick off a new thread t.start(); // running WriteY() // Simultaneously, do something on the main thread. for (int i = 0; i < 1000; i++) Console.Write ("x"); static void WriteY() for (int i = 0; i < 1000; i++) Console.Write ("y"); C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 6 / 24

Threading: basics class ThreadTest static void Main() Thread t = new Thread (WriteY); // Kick off a new thread t.start(); // running WriteY() // Simultaneously, do something on the main thread. for (int i = 0; i < 1000; i++) Console.Write ("x"); static void WriteY() for (int i = 0; i < 1000; i++) Console.Write ("y"); C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 7 / 24

How does it work? Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. A thread scheduler ensures all active threads are allocated appropriate execution time, and that threads that are waiting or blocked do not consume CPU time. On a single-processor computer, a thread scheduler performs time-slicing rapidly switching execution between each of the active threads. On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency, where different threads run code simultaneously on different CPUs. C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 8 / 24

Threads vs Processes A thread is analogous to the operating system process in which your application runs. Just as processes run in parallel on a computer, threads run in parallel within a single process. Processes are fully isolated from each other; threads have just a limited degree of isolation. C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 9 / 24

Creating and starting threads Threads are created using the Thread class s constructor, passing in a ThreadStart delegate which indicates where execution should begin. Calling Start on the thread then sets it running. The thread continues until its method returns, at which point the thread ends. Creating threads: static void Main() Thread t = new Thread (new ThreadStart (Go)); Thread t2 = new Thread (Go); //The same = just shorter syntax t.start(); // Run Go() on the new thread. t2.start(); static void Go() Console.WriteLine ("hello!"); C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 10 / 24

C# 2017/2018, Lecture 13 11 / 24 Task 1: Crating threads Create new Console Application. Write simple function public static void Increment() which prints 10 numbers: 0 1 2 3 4 5 6 7 8 9 in a for loop In Main function: Create and start new threads t1 and t2 using both methods (as in the example from previous slide) that run Increment function. Run Increment() from the main thread. In the end of the program print Main thread at the end and wait for the key from the user

Naming threads Each thread has a Name property that you can set for the benefit of debugging. This is particularly useful in Visual Studio, since the thread s name is displayed in the Threads Window and Debug Location toolbar. You can set a thread s name just once; attempts to change it later will throw an exception. The static Thread.CurrentThread property gives you the currently executing thread. In the following example, we set the main thread s name: class ThreadNaming static void Main() Thread.CurrentThread.Name = "main"; Thread worker = new Thread (Go); worker.name = "worker"; worker.start(); Go(); static void Go() Console.WriteLine ("Hello from " + Thread.CurrentThread.Name); http://www.albahari.com/threading/ C# 2017/2018, Lecture 13 12 / 24

C# 2017/2018, Lecture 13 13 / 24 Task 2: Naming Name all the threads (including the main thread) so that they are easily identifiable. In Increment method add information which thread prints the number Do the same for the next task (Decrement)

Passing data to a thread The easiest way to pass arguments to a thread s target method is to execute a lambda expression that calls the method with the desired arguments: Thread t = new Thread ( () => Print ("Hello from t!") ); t.start(); You can do the same thing almost as easily in C# 2.0 with anonymous methods: Thread t = new Thread (delegate() Print ("Hello from t!"); ); t.start(); C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 14 / 24

C# 2017/2018, Lecture 13 15 / 24 Task 3: Threads with parameters Write simple Decrement function public static void Decrement(int max) which prints numbers from max to 0 in a for loop In Main function Create and start threads t3 and t4 which print numbers from 5 to 0 (t3) and 10 to 0 (t4). Threads should be created using both mentioned methods.

Join and Sleep You can wait for another thread to end by calling its Join method. For example: static void Main() Thread t = new Thread (Go); t.start(); t.join(); Console.WriteLine ("Thread t has ended!"); static void Go() for (int i = 0; i < 1000; i++) Console.Write ("y"); This prints y 1,000 times, followed by Thread t has ended! immediately afterward. You can include a timeout when calling Join, either in milliseconds or as a TimeSpan. It then returns true if the thread ended or false if it timed out. Thread.Sleep pauses the current thread for a specified period: Thread.Sleep (TimeSpan.FromHours (1)); // sleep for 1 hour Thread.Sleep (500); // sleep for 500 milliseconds While waiting on a Sleep or Join, a thread is blocked and so does not consume CPU resources. C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 16 / 24

C# 2017/2018, Lecture 13 17 / 24 Task 4: Join & Sleep In the Increment method sleep for 10 ms after printing each number. Using Join method order main thread to wait for all the others (t1,t2,t3,t4) at the end of the program to finish.

C# 2017/2018, Lecture 13 18 / 24 Abort By default threads die after they fulfilled their function (when they encounter the end of function) All blocking methods (such as Sleep, Join, EndInvoke, and Wait) block forever if the unblocking condition is never met and no timeout is specified. Occasionally, it can be useful to release a blocked thread prematurely; for instance, when ending an application. Two methods accomplish this: Thread.Interrupt (almost never needed) Thread.Abort A blocked thread can be forcibly released via its Abort method. ThreadAbortException is thrown. Thread should catch it in the try catch block. Program does not kill threads; it rather nicely asks (using an exception) for a suicide.

Exceptions and threads Catching exceptions in threads should always be handled inside the function the thread is starting (certainly not outside) public static void Main() new Thread (Go).Start(); static void Go() try //normal thread execution catch (Exception ex) // Typically log the exception, and/or signal another thread // that we've come unstuck //... C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 19 / 24

C# 2017/2018, Lecture 13 20 / 24 Task 5: Abort In the Increment and Decrement function add exception handling. In case of finding signal from Abort method thread should print text Thread [name] Aborted! and finish. In case of other exceptions, program should print the name of the exception. Abort t1 and t4 Threads in the Main function.

Locks Exclusive locking is used to ensure that only one thread can enter particular sections of code at a time. Other threads wait untill the lock becomes available. class ThreadSafe static bool done; static readonly object locker = new object(); static void Main() new Thread (Go).Start(); Go(); static void Go() lock (locker) if (!done) Console.WriteLine ("Done"); done = true; C# 2017/2018, Lecture 13 http://www.albahari.com/threading/ 21 / 24

C# 2017/2018, Lecture 13 22 / 24 Task 6: Locks Add to the program global variable static int GLOBAL_VALUE = 0; Increase GLOBAL_VALUE by 1 in Increment function and decrease by 1 in Decrement function (see next point) Change Increment and Decrement functions to increase a chance of problems: Create int tmp = GLOBAL_VALUE; variable in the beginning; increment/decrement it Rewrite GLOBAL_VALUE = tmp; in the end of the for loop Print the GLOBAL_VALUE value at the end of the program. Observe variations from number 5 when running program. Add locks to deal with the created problem.

C# 2017/2018, Lecture 13 23 / 24 References Only one, but highly recommended: http://www.albahari.com/threading

THE END dr inż. Małgorzata Janik majanik@if.pw.edu.pl