Multithreaded Programming in Java David Meredith Aalborg University.

Slides:



Advertisements
Similar presentations
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
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.
Multithreading The objectives of this chapter are:
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
More Multithreaded Programming in Java David Meredith Aalborg University.
Multithreading.
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.
What is Concurrent Programming? Maram Bani Younes.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
1 Web Based Programming Section 8 James King 12 August 2003.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Internet Software Development Controlling Threads Paul J Krause.
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.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
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.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
1 Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming.
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.
CSC 480 Software Engineering Lab 2 – Multi-Threading Oct 18, 2002.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
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:
A brief intro to: Parallelism, Threads, and Concurrency
Doing Several Things at Once
Multithreading / Concurrency
Multi Threading.
Multithreading.
PA1 Discussion.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
Chapter 19 Java Never Ends
Multithreading Chapter 23.
Multithreading.
Multithreaded Programming
What is Concurrent Programming?
Programming with Shared Memory Java Threads and Synchronization
What is Concurrent Programming?
Multithreading in java.
Threads and Multithreading
Lecture 19 Threads CSE /6/2019.
Multithreaded Programming in Java
Multithreading The objectives of this chapter are:
CMSC 202 Threads.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Multithreaded Programming in Java David Meredith Aalborg University

Sources Chapter 14 of The Java Programming Language (Fourth Edition) by Ken Arnold, James Gosling and David Holmes (Addison-Wesley, 2006)

Single-threaded programming Single-threaded program –single sequence or thread of instructions, executed one after the other –only one instruction being carried out at any given instant in time –the single thread in a single- threaded program is called the main thread

SingleThreadedProgram.java

Multithreaded programming Most of the programs you use every day are multithreaded programs because they have to do two or more things at the same time For example –word processor has to listen for user input, update the screen, save periodic backups, look-up typed words in a dictionary as they are typed, etc. –web crawler crawls many different web pages simultaneously, creating a record for each one in the search engine’s database

Multithreaded programming Multithreaded program consists of more than one independent thread (sequence of instructions), running at the same time or concurrently –hence sometimes called concurrent programming Two or more threads can access the same data –this is good in a web crawler where many different crawling threads can update the same central database –HOWEVER this can cause problems such as the race condition

Race Condition Suppose a is the joint bank account of Hr. and Fru Jensen and it starts off with a balance of 1000kr Hr. Jensen goes to the branch near his work and deposits 100 kr Fru Jensen goes to the branch near their home and withdraws 50 kr The final balance should be 1050 kr...but is it? It depends on whether the account is locked while a transaction is taking place

Creating Threads in Java Create a new thread of control in Java by instantiating a Thread object: Thread worker = new Thread(); Can then set its initial priority, name, etc.: worker.setPriority(worker.getPriority()+1); worker.setName(“myThread”); When ready, call the Thread’s start method to set it off worker.start(); –This starts a new thread of control based on data in the Thread object VM invokes new Thread’s run method which makes the Thread active Thread’s run method does nothing, but can override it in a subclass of Thread

PingPong.java

Creating threads by specializing Thread In PingPong, we defined a subclass of Thread called PingPongThread and redefined the run method in this subclass to specify precisely what PingPongThreads should do But what if we had wanted PingPongThread to inherit from a class other than Thread? –We couldn’t, because it has to inherit from Thread There’s a better way...

Using the Runnable interface A Thread abstracts the concept of a worker –i.e., something that carries out some task When you subtype Thread, the task carried out by the new subtype is defined in its run() method Instead, can define a new class that implements the Runnable interface Runnable interface abstracts the concept of a task Runnable interface declares a method public void run(); A class that implements Runnable must implement this run() method A Thread (worker) can be assigned a Runnable object (task) to run –The Thread’s run() method calls the run() method of the Runnable object assigned to it when the Thread is started (with the start() method)

PingPong2.java

Thread runs Runnable objects and implements Runnable itself

Interference and Critical Regions Interference is when interleaved operations from different threads on shared data could corrupt the data The code segments that contain the interfering actions are called critical sections or critical regions

Synchronization and Locks Prevent interference by synchronizing access to critical regions A Thread should have to acquire a lock on a shared object before processing it –Here, would have to acquire a lock on the bank account, a, before changing it Thread releases lock on an object when finished with it A synchronized method or statement acquires a lock on an object before it executes and releases the lock once it has finished

Synchronized Methods HrOgFruJensen.java

Synchronized Statements HrOgFruJensen2.java