Why Be Concurrent? CET306 Harry R. Erwin University of Sunderland.

Slides:



Advertisements
Similar presentations
How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Advertisements

Extreme Programming Alexander Kanavin Lappeenranta University of Technology.
Concurrent and Distributed Systems Introduction to CET306 Harry R. Erwin, PhD University of Sunderland.
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Development Techniques CSE301 University of Sunderland Harry R. Erwin, PhD.
Rules for Designing Multithreaded Applications CET306 Harry R. Erwin University of Sunderland.
PS4: Test Driven Development Based on Test Driven Development by Example By Kent Beck.
Threads in C# Threads in C#.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
1 Tuesday, November 07, 2006 “If anything can go wrong, it will.” -Murphy’s Law.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Programming in Java CSE301 Half Lecture Harry Erwin, PhD University of Sunderland.
1 New Architectures Need New Languages A triumph of optimism over experience! Ian Watson 3 rd July 2009.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Tahir Nawaz Visual Programming C# Week 2. What is C#? C# (pronounced "C sharp") is an object- oriented language that is used to build applications for.
TDD OVERVIEW OF TEST DRIVEN DEVELOPMENT by Paul M. code of the damned. com.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Finding Concurrency CET306 Harry R. Erwin University of Sunderland.
What is Refactoring? CSE301 University of Sunderland Harry R. Erwin, PhD.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Oct Multi-threaded Active Objects Ludovic Henrio, Fabrice Huet, Zsolt Istvàn June 2013 –
Multicore Systems CET306 Harry R. Erwin University of Sunderland.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Refactoring Improving the structure of existing code Refactoring1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Problem of the Day  Why are manhole covers round?
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
COMP 121 Week 1: Testing and Debugging. Testing Program testing can be used to show the presence of bugs, but never to show their absence! ~ Edsger Dijkstra.
Chapter 22 Developer testing Peter J. Lane. Testing can be difficult for developers to follow  Testing’s goal runs counter to the goals of the other.
Games Development 2 Concurrent Programming CO3301 Week 9.
Software Engineering Chapter 3 CPSC Pascal Brent M. Dingle Texas A&M University.
Refactoring1 Improving the structure of existing code.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Chapter 7 The Practices: dX. 2 Outline Iterative Development Iterative Development Planning Planning Organizing the Iterations into Management Phases.
Extreme Programming (XP) XP is an agile methodology: –aims to be responsive to change Theme running through XP is the importance of communication –amongst.
The Software Development Process
Upcoming Presentations ILM Professional Service – Proprietary and Confidential ( DateTimeTopicPresenter March PM Distributed.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
University of Sunderland CSE301 Advanced Object-Oriented Software DevelopmentUnit 1 Test-Driven Development CSE301 University of Sunderland Dr. Giles Oatley.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Continuous Improvement. Start Simple and Continually Improve E.g., Gmail Labels 1.
Proving Correctness and Measuring Performance CET306 Harry R. Erwin University of Sunderland.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Multi Threading.
Unit testing C# classes
Improving the structure of existing code
Foundations and Definitions
Presentation transcript:

Why Be Concurrent? CET306 Harry R. Erwin University of Sunderland

Roadmap Why Concurrency? Test Driven Development Refactoring Threading in C#

Texts Clay Breshears (2009) The Art of Concurrency: A Thread Monkey's Guide to Writing Parallel Applications, O'Reilly Media, Pages: 304. This is available on as a paperback and more cheaply as a Kindle edition. You don't need a Kindle to read a Kindle edition. Mordechai Ben-Ari (2006) Principles of Concurrent and Distributed Programming, Addison-Wesley. Also available on This is available as a paperback only. Covers the theory in depth and will be used as a supplementary text.

Want to Go Faster? I assume you want to write faster code. – You need to be prepared to write the code. – You need to know the syntax. – You need to know the libraries and methods. – You need to be prepared to think carefully and logically. – You need intuition.

Definitions A system is concurrent if it can time-share. A system is parallel if it can do more than one thing simultaneously. This is a subset of concurrent.

Why Concurrency? Multi-core processors are now the state of the art—multiple CPUs running on a shared memory with shared resources. The old pattern of Moore’s Law no longer applies 8(. To run faster, you have to share the work among multiple processors.

A Threading Methodology 1.Analysis—similar to specification, where you identify the code that can run concurrently. As a rule, start with working and already tuned code. Debugging concurrent code is a nightmare. You will use algorithm analysis and a profiler. 2.Design and implementation—do it. The book is mostly about this step. 3.Test for correctness. If the design is provably correct, you avoid much pain here. 4.Tune for performance. Use a profiler and check how well the code is running. Your threading may actually be affecting your tuning.

Testing and Tuning If you modify your code to improve its performance, you can introduce standard bugs and threading bugs. Standard bugs are the bugs you can often detect using test-driven design. It’s useful to have a collection of unit tests around to check for them. Threading bugs are the really nasty ones, because they depend on thread ordering. Design defensively to avoid these bugs. If you can prove your design is correct, you don’t have to do exhaustive (and exhausting) thread testing.

Test-Driven Development and Refactoring Test-Driven Development Unit Testing A Catalogue of Refactorings

Sources Fowler, 2000, Refactoring: Improving the Design of Existing Code, Addison-Wesley. Available electronically at the library. / / / / / / ex.php ex.php Extensive discussions on the comp.object newsgroup.

Test-Driven Development Refactoring depends on having good unit tests. This means you need to be doing test- driven development. Test-driven development is a method of software development where tests specify interfaces of implementation and all code must have passed the tests. (Wikipedia)

How Bob Martin Describes TDD (personal communication) Erwin: TDD as I understand it: 1. Write a test for a bit of functionality. 2. Show that it fails. 3. Write the code to make the test pass. Martin: A good summary, but there's more. 1. We do not write production code until there is a failing test. 2. We write the simplest possible production code to get the test to pass. 3. We do not write more tests when we have a failing test. 4. We do not add to a failing test.

Martin Comments Further If you watched someone doing TDD you would see them oscillating between test code and production code once every minute or so. During each oscillation the programmer would add a few lines to his test code, thus making it fail (or not compile) and then add just a few lines to his production code in order to make the test pass (or compile). Each oscillation is so simple that it's not worth taking. Each oscillation is so simple that the risk of error is close to zero. If you walk into a room of people working this way, and chose anyone at random, a minute ago all his code would have been working.

Automated Unit Testing Originally defined for Java (Junit). Ported to.Net as NUnit See Also see ing_frameworks ing_frameworks I strongly recommend use of such a framework.

Refactoring Deals with code rot. Your program works but it has become ugly as it has been modified. “Refactoring is a technique to restructure code in a disciplined way. For a long time it was a piece of programmer lore, done with varying degrees of discipline by experienced developers, but not passed on in a coherent way.” (Fowler)

Refactoring Principles Change only the implementations Use it to improve the design of software Use it to make software more understandable Use it to help find bugs Use it to help you program faster Use it to prepare your current program for modification.

When? “Three strikes and you refactor.” – The third time you have to cope with something ugly, fix it. particularly… – When you add functionality – When you need to fix a bug – When you do a code review

The Basic Rule of Refactoring “Refactor the low hanging fruit” uit uit Low Hanging Fruit (def): “The thing that gets you most value for the least investment.” In other words, don’t spend much time on it. There are always ways to improve any design incrementally. We will explore a few of them.

The Goal of Refactoring To improve code without changing what it does. This in some ways is similar to how an optimizing compiler restructures code.

Kinds of Refactorings Composing Methods Moving Features Between Objects Organizing Data Simplifying Conditionals Making Method Calls Simpler Generalization Big Refactorings

Recommendations Do some reading on Test Driven Development. Choose and learn to use a unit testing framework. Do more reading on refactoring in C#. The Fowler book is for Java, but the same techniques carry over to C# Learn the features supporting refactoring in Visual Studio 2010.

Refactoring Conclusions Kent Beck says: This is “only the beginning.” Why? Questions we’ve left unaddressed include when to use refactoring and when to let well enough alone. Beck’s advice: – Pick an achievable goal – Stop when you’re unsure – Backtrack if necessary – Work with a partner

Background of Parallel Algorithms Next Week

Threading in C# Creating and running threads Managing threads Synchronisation

General To work with threads in C#, you need to import System and System.Threading. The code is: using System; using System.Threading; Some key classes are Thread and ThreadStart.

Creating and Running Threads In C# you use a ThreadStart delegate to create a Thread. The argument to ThreadStart is the name of a method that returns void and will be the run() method of the thread. The Thread constructor can implicitly wrap its argument in a ThreadStart delegate. Thread can also take a lambda expression as an argument. – Thread t = new Thread( ()=>aMethod(arguments));

Example PseudoCode Namespace whatever{ class Foo { // various variables and constructors public void Dowhatever() {etc.} // the run method static void Main(String[] args){ Foo f = new Foo(whatever); Thread t = new Thread(new ThreadStart(f.Dowhatever); t.start(); }

Managing Threads This is where the various Thread methods come in. (next slide) The ThreadPool class is available and is used to make efficient use of multiple threads. You queue work items in the ThreadPool and provide callback delegates to be executed when the work items complete. We’ll get into this in more detail later.

Thread Interesting members include: – Thread.Sleep(msec); —note this is class level— – Name(string); —give it a name— – Priority(ThreadPriority); —give it a priority— – Abort(); —die— – Interrupt(); —wake immediately— – Join(); —have the calling thread wait until exit— – Resume(); —unsuspend— – Start(); —start the thread— – Suspend(); —take a break—

Synchronisation If threads share data, access must be managed to avoid conflicts. One class used to manage this is the Monitor class. If you want to lock an Object, call Monitor.Enter(Object). When you’re done with it, remember to call Monitor.Exit(Object). This needs to be packaged in a try{} finally{} construct to avoid errors making locks permanent. The easiest way to do this is lock(this){etc.} which hides the hassle from you.

Conclusion The tutorial today plays around with these ideas. See you there!