1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Multithreading. RHS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 Web Based Programming Section 8 James King 12 August 2003.
Games Development 2 Concurrent Programming CO3301 Week 9.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Concurrent Programming and Threads Threads Blocking a User Interface.
C20: Threads see also: ThreadedBallWorld, DropTest, Tetris source examples Not covered: advanced stuff like notify/notifyAll.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
1 OS Review Processes and Threads Chi Zhang
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Multithreaded Programming in Java David Meredith Aalborg University.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.
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.
Multithreading. DCS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
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.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Java Thread Programming
Principles of Software Development
Chapter 4: Multithreaded Programming
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Multi Threading.
PA1 Discussion.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Multithreading.
Multithreading.
Dr. Mustafa Cem Kasapbaşı
Chapter 4: Threads & Concurrency
Java Concurrency.
Java Concurrency.
Threads and Multithreading
Java Concurrency 29-May-19.
Lecture 19 Threads CSE /6/2019.
Multithreaded Programming in Java
CMSC 202 Threads.
More concurrency issues
Presentation transcript:

1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013

Overview Why Concurrency? Threads Scheduling Runnable interface Race Conditions Locks Thread Safe Dead Lock 2Copyright © Texas Education Agency, 2013

Why Concurrency? There are times when you write a program that you want to do more than one thing at a time: Draw to the screen while getting input from the keyboard or mouse Continuously redraw the screen while making calculations (e.g. A progress bar) Have an AI “determine its move” while you are making your move Saving a file while working on a document IT: Advanced Computer Programming – Concurrency33Copyright © Texas Education Agency, 2013

Why Concurrency? Java natively supports doing more than one thing at once with a robust concurrency library. Concurrency allows multiple tasks to be accomplished at once using threads of execution. IT: Advanced Computer Programming – Concurrency44Copyright © Texas Education Agency, 2013

Threads Threads are the computer processes that execute tasks from beginning to end. If you’ve written a program, you’ve used a thread. The main() method in Java is an example of a thread. It begins execution with the first instruction in main and continues until it reaches the last instruction in the method (or an infinite loop if you accidentally create one). IT: Advanced Computer Programming – Concurrency55Copyright © Texas Education Agency, 2013

Scheduling – Single Processor The magic behind concurrency is scheduling. Normally, a computer will determine the details of how a multithreaded program runs. For example, if you only have one processor on a computer, clearly only one process or task can run at a time. What the computer does to overcome this is “time share”, or allow one process to run for a little while, stop, and let another process run for a little while. This continues until all processes cease functioning. IT: Advanced Computer Programming – Concurrency66Copyright © Texas Education Agency, 2013

Scheduling – Multicore Some operating systems allow you to take advantage of multicore processors which give programs the opportunity to simultaneously run as many processes as there are cores. This allows multithreaded programs to execute much faster. IT: Advanced Computer Programming – Concurrency77Copyright © Texas Education Agency, 2013

Runnable interface In Java, the most basic tasks implement the Runnable interface which is passed to a Thread. The Runnable interface requires that you implement a run() method. The run() method is where you provide the task to be executed in the thread. IT: Advanced Computer Programming – Concurrency88Copyright © Texas Education Agency, 2013

Runnable interface In Java, the most basic tasks implement the Runnable interface which is passed to a Thread. The Runnable interface requires that you implement a run() method. The run() method is where you provide the task to be executed in the thread. IT: Advanced Computer Programming – Concurrency99Copyright © Texas Education Agency, 2013

Runnable interface public class Task implements Runnable { public Task() {} public void run() { // do stuff here } IT: Advanced Computer Programming – Concurrency10 Copyright © Texas Education Agency, 2013

Race Conditions One of the risks of working with multithreaded programs is when two threads try to use variables on the same object at the same time. This is called a race condition. For example, one thread accesses a car object. This thread stops running and a second thread takes over and deletes the car object. The second thread finishes and the first thread continues executing and tries to access a value on the now deleted car. IT: Advanced Computer Programming – Concurrency11 Copyright © Texas Education Agency, 2013

Locks To solve race conditions, you can use locks. A lock is a piece of code that prevents other threads from accessing an object until the locking thread is done with it. In Java, this is accomplished with the synchronized keyword. IT: Advanced Computer Programming – Concurrency12 Copyright © Texas Education Agency, 2013

Locks public class BankAccount { //… // synchronized allows deposits to // be entered safely public synchronized deposit(int amount) { balance = balance + amount; } // … } IT: Advanced Computer Programming – Concurrency13 Copyright © Texas Education Agency, 2013

Thread Safe A function that uses the synchronized keyword is said to be thread safe. There are data structures in Java that are thread safe: Vector, Stack and Hashtable. Other data structures can be made thread safe, examine the Java API documentation for details. IT: Advanced Computer Programming – Concurrency14 Copyright © Texas Education Agency, 2013

Dead Lock Multithreaded programs can be very difficult to debug and one of the most difficult problems is dead lock. Dead lock occurs when two functions try to access two different resources. One function will have a lock on one object, the other function will have a lock on the other object -- and they are both waiting for each other to let go of their lock. Often, this can be mistaken as an infinite loop. IT: Advanced Computer Programming – Concurrency15 Copyright © Texas Education Agency, 2013

Dead Lock The best way to prevent dead lock is to ensure that you always access objects and resources in the same order. IT: Advanced Computer Programming – Concurrency16 Copyright © Texas Education Agency, 2013