Multi-threaded programming in Java

Similar documents
wait with priority An enhanced version of the wait operation accepts an optional priority argument:

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

Synchronization. Clock Synchronization

OUTLINE. Introduction Clock synchronization Logical clocks Global state Mutual exclusion Election algorithms Deadlocks in distributed systems

Synchronization Part 1. REK s adaptation of Claypool s adaptation of Tanenbaum s Distributed Systems Chapter 5

Clock Synchronization. Synchronization. Clock Synchronization Algorithms. Physical Clock Synchronization. Tanenbaum Chapter 6 plus additional papers

Chapter 6 Synchronization

CMSC 330: Organization of Programming Languages

Advanced Topics in Distributed Systems. Dr. Ayman A. Abdel-Hamid. Computer Science Department Virginia Tech

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

ICT 6544 Distributed Systems Lecture 7

Time in Distributed Systems

Event Ordering. Greg Bilodeau CS 5204 November 3, 2009

Concurrent Programming using Threads

Concurrency in Java Prof. Stephen A. Edwards

Java Threads. COMP 585 Noteset #2 1

CMSC 714 Lecture 14 Lamport Clocks and Eraser

Synchronization. Chapter 5

public class Shared0 { private static int x = 0, y = 0;

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

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

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

CMSC 330: Organization of Programming Languages

CSE 5306 Distributed Systems

Last Class: Naming. Today: Classical Problems in Distributed Systems. Naming. Time ordering and clock synchronization (today)

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

殷亚凤. Synchronization. Distributed Systems [6]

CS11 Java. Fall Lecture 7

Threads and Java Memory Model

What is a thread anyway?

Introduction to Java Threads

Process Synchronization

Programming in Parallel COMP755

CSE 5306 Distributed Systems. Synchronization

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Models of concurrency & synchronization algorithms

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

Threads Questions Important Questions

Distributed Synchronization. EECS 591 Farnam Jahanian University of Michigan

Lecture 9: Introduction to Monitors

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

SYNCHRONIZATION. DISTRIBUTED SYSTEMS Principles and Paradigms. Second Edition. Chapter 6 ANDREW S. TANENBAUM MAARTEN VAN STEEN

Time Synchronization and Logical Clocks

TIME ATTRIBUTION 11/4/2018. George Porter Nov 6 and 8, 2018

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

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Lecture 7: Process & Thread Introduction

CIS233J Java Programming II. Threads

DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK. UNIT I PART A (2 marks)

Lecture 8: September 30

Basics of. Multithreading in Java

Clock and Time. THOAI NAM Faculty of Information Technology HCMC University of Technology

Java Threads. Introduction to Java Threads

Arranging lunch value of preserving the causal order. a: how about lunch? meet at 12? a: <receives b then c>: which is ok?

Component-Based Software Engineering

CSE 124: LAMPORT AND VECTOR CLOCKS. George Porter October 30, 2017

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Note: in this document we use process and thread interchangeably.

CPS 506 Comparative Programming Languages. Programming Language Paradigms

EECS 482 Introduction to Operating Systems

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

What is a Thread? Why Multicore? What is a Thread? But a fast computer runs hot THREADS AND CONCURRENCY. Concurrency (aka Multitasking)

COMP 346 WINTER Tutorial 5 MONITORS

Chapter 32 Multithreading and Parallel Programming

Synchronization for Concurrent Tasks

What's wrong with Semaphores?

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

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

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Advanced Concepts of Programming

Chapter 13. Concurrency ISBN

Unit 8: Threads. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

THREADS AND CONCURRENCY

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

CS 351 Design of Large Programs Threads and Concurrency

Chapter 5 Asynchronous Concurrent Execution

Concurrency: a crash course

Synchronization in Concurrent Programming. Amit Gupta

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

The University of Texas at Arlington

Time Synchronization and Logical Clocks

CS 10: Problem solving via Object Oriented Programming Winter 2017

Synchronization COMPSCI 386

Chapter 6: Process Synchronization

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

Distributed Systems Exam 1 Review. Paul Krzyzanowski. Rutgers University. Fall 2016

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Synchronization Spinlocks - Semaphores

Programming Language Concepts: Lecture 11

Lecture 7: Logical Time

Distributed Coordination 1/39

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

Transcription:

Multi-threaded programming in Java Java allows program to specify multiple threads of execution Provides instructions to ensure mutual exclusion, and selective blocking/unblocking of threads

What is a thread in Java? A thread is a program-counter and a stack All threads share the same memory space A running thread can Yield Sleep Wait for I/O or notification Be pre-empted A key feature: Synchronized methods Allow an exclusive lock, e.g., in an update method

Basic Syntax Build a thread by extending the class java.lang.thread Must have a public void run() method it is executed at the start of the thread, and when it finishes, the thread finishes Synchronized statement Synchronized (obj) { block } Obtains a lock on obj before executing block, releases lock after executing block Wait() gives up lock and suspends the thread Notifyall() resumes all threads waiting on object, resumed tasks must reacquire lock before continuing

Producer Consumer Example Public class ProdCons { private boolean ready ; private Object obj ; public ProducerConsumer() { ready = false ; } public ProducerConsumer (Object o) { obj = o ; ready = true ; } Synchronized Object consume() { while (!ready) wait() ; ready = false ; notifyall() ; return obj ; } Synchronized void produce (object o) { while (ready) wait() ; obj = o ; ready = true ; notifyall() ; } }

Introduction to Distributed Systems First two topics Conceptual Issue: Local and Global Clocks Practical Issue: Process Communication Distributed Mutual Exclusion

Absence of Global Clock Problem: synchronizing the activities of different part of the system (e.g. process scheduling) What about using a single shared clock? two different processes can see the clock at different times due to unpredictable transmission delays What about using radio synchronized clocks? Propagation delays are unpredictable Software approaches Clock synchronization algorithms Logical clocks

Cristian's Algorithm Basic idea: get the current time from a time server. Issues: Error due to communication delay - can be estimated as (T 1 -T 2 -I)/2 Time correction on client must be gradual

The Berkeley Algorithm a) The time daemon asks all the other machines for their clock values b) The machines answer c) The time daemon tells everyone how to adjust their clock

Logical clocks The need to order events in a distributed system has motivated schemes for logical clocks These artificial clocks provide some but not all of the functionality of a real global clock They build a clock abstraction based on underlying physical events of the system

Happened before relation: definitions Happened before relation ( ): a b if a and b are in the same process and a occurred before b a b if a is the event of sending a message and b is the event of receiving the same message by another process if a b and b c then a c, i.e. the relation is transitive The happened before relation is a way of ordering events based on the behavior of the underlying computation

Happened before relation: definitions (2) Two distinct events a and b are said to be concurrent (a b) if and For any two events in the system, either a b, b a or a b Example: a / b b / a P 1 e 11 e 12 e 13 e 14 e 11 e 21 e 22 e 13, e 13 e 14 thus e 22 e 14 P 2 e 21 e 22 e 23 e 24 Global Time

Lamport s Logical Clocks: definitions A logical clock C i at each process P i is a function that assigns a number C i (a) to any event a, called timestamp timestamps are monotonically increasing values example: C i (a) could be implemented as a counter We want to build a logical clock C(a) such that: if a b then C(a) < C(b)

Lamport s Logical Clocks: implementation If we want a logical clock C(a) to satisfy: if a b then C(a) < C(b) the following conditions must be met: if a and b are in the same process and a occurred before b, then C i (a) < C i (b) if a is the event of sending a message in process P i and b is the event of receiving the same message by process P j then C i (a) < C j (b)

Lamport s Logical Clocks: implementation (2) Two implementations rules that satisfy the previous correctness conditions are: clock C i is incremented by d at each event in process P i: C i := C i + d (d > 0) if event a is the sending of a message m by process P i, then message m is assigned the timestamp t m = C i (a) (C i (a) is obtained after applying previous rule). Upon receiving message m, process P j sets its clock to: C j := max(c j, t m + d) (d > 0)

Lamport s Logical Clocks: example Fill the blanks e 11 e 12 e 13 e 14 e 15 e 16 e 17 P 1 ( ) ( ) ( ) ( ) ( ) ( ) ( ) P 2 ( ) ( ) ( ) ( ) ( ) e 21 e 22 e 23 e 24 e 25 Global Time

Lamport s Logical Clocks: example e 11 e 12 e 13 e 14 e 15 e 16 e 17 P 1 (1) (2) (3) (4) (5) (6) (7) P 2 (1) (2) (3) (4) (7) e 21 e 22 e 23 e 24 e 25 Global Time