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.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Synchronization and Deadlocks
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Multithreading The objectives of this chapter are:
Software Engineering Oct-01 #11: All About Threads Phil Gross.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
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.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Java Programming: Advanced Topics
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.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
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.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Threads.
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Threads Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment?
Java Thread and Memory Model
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.
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.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Multithreading The objectives of this chapter are:
Multithreading / Concurrency
Multithreading Lec 23.
Multi Threading.
Background on the need for Synchronization
Multithreaded Programming in Java
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading.
Multithreading.
Multithreaded Programming
Lecture 2 Part 2 Process Synchronization
Java Concurrency 17-Jan-19.
Java Concurrency.
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Java Concurrency.
Threads and Multithreading
Java Concurrency 29-May-19.
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Threads and concurrency / Safety
Synchronization and liveness
Presentation transcript:

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 of the program (memory, resources, etc.)  Several threads can run “at the same time” What does this mean?  Every Swing/AWT program has at least two threads AWT/event thread Main program thread l Coordinating threads is complicated  Deadlock, starvation/fairness  Monitors for lock/single thread access

Software Design 13.2 Concurrent Programming l Typically must have method for ensuring atomic access to objects  If different threads can read and write the same object then there is potential for problems ThreadTrouble.java example Consider getting x and y coordinates of a moving object  If an object is read-only, there are no issues in concurrent programming String is immutable in Java, other classes can have instance variables be defined as final, cannot change (like const) l In Java, the keyword synchronized is the locking mechanism used to ensure atomicity  Uses per-object monitor (C.A.R. Hoare), processes wait to get the monitor, it’s re-entrant

Software Design 13.3 Using synchronized methods l Methods can be synchronized, an object can be the argument of a synchronized block, a class cannot be synchronized  Every object has a lock, entering a synchronized method of the object, or using the object in a synchronized block, blocks other threads from using synchronized methods of the object (since the object is locked)  If a synchronized method calls another synchronized method on the same object, the lock is maintained (even recursively)  Another thread can execute any unsynchronized method of an object O, even if O’s lock is held  A thread blocks if it tries to execute a synchronized method of an object O if O’s lock is held by a different thread

Software Design 13.4 Thread classes in Java Classes can extend java.lang.Thread or implement java.lang.Runnable, (note: Thread implements Runnable)  A thread’s run method is executed when the thread is started  Typically the run method is “infinite” Executes until some final/done state is reached The run method must call sleep(..) or yield(); if not the thread is selfish and once running may never stop  A runnable object is run by constructing a Thread object from the runnable and starting the thread l Threads have priorities and groups  Higher priority threads execute first  Thread groups can be a useful organizational tool