CS 556 Distributed Systems

Similar documents
CIS233J Java Programming II. Threads

Multithreaded Programming

7. MULTITHREDED PROGRAMMING

Java Threads. Introduction to Java Threads

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

Unit - IV Multi-Threading

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Multithread Computing

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

Object Oriented Programming (II-Year CSE II-Sem-R09)

Concurrent Programming using Threads

Note: Each loop has 5 iterations in the ThreeLoopTest program.

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Multithreading Pearson Education, Inc. All rights reserved.

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

Concurrent Programming

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

Threads Chate Patanothai

By: Abhishek Khare (SVIM - INDORE M.P)

UNIT IV MULTITHREADING AND GENERIC PROGRAMMING

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Amity School of Engineering

Basics of. Multithreading in Java

Techniques of Java Programming: Concurrent Programming in Java

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

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

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

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

Performance Throughput Utilization of system resources

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Java s Implementation of Concurrency, and how to use it in our applications.

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

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Introduction to Java Threads

Definition: A thread is a single sequential flow of control within a program.

Module - 4 Multi-Threaded Programming

Advanced Programming Concurrency

Java Threads Instruct or: M Maina k Ch k Chaudh dh i ur ac.in in 1

Object Oriented Programming. Week 10 Part 1 Threads

04-Java Multithreading

Animation Part 2: MoveableShape interface & Multithreading

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

User Space Multithreading. Computer Science, University of Warwick

Advanced Concepts of Programming

Software Practice 1 - Multithreading

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

CS 351 Design of Large Programs Threads and Concurrency

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

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

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

JAVA. Lab 12 & 13: Multithreading

Multi-threading in Java. Jeff HUANG

Unit III Rupali Sherekar 2017

Object-Oriented Programming Concepts-15CS45

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

G52CON: Concepts of Concurrency

Java Threads Vs Processes. CS Concurrent Programming. Java Threads. What can a thread do? Java Concurrency

Java Threads. COMP 585 Noteset #2 1

What is a thread anyway?

UNIT V CONCURRENT PROGRAMMING

CS360 Lecture 12 Multithreading

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Multithreaded Programming

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Multithreading using Java. Dr. Ferdin Joe John Joseph

Threads in Java. Threads (part 2) 1/18

What is a Thread? Individual and separate unit of execution that is part of a process. multiple threads can work together to accomplish a common goal

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

MULTI-THREADING

CS 159: Parallel Processing

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

Concurrent Computing CSCI 201 Principles of Software Development

CS180 Review. Recitation Week 15

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications


COMP346 Winter Tutorial 4 Synchronization Semaphores

Robotics and Autonomous Systems

Lecture 7: Process & Thread Introduction

Concurrency in Java Prof. Stephen A. Edwards

Model Requirements and JAVA Programs MVP 2 1

THREADS AND MULTITASKING ROBOTS

ROBOTICS AND AUTONOMOUS SYSTEMS

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

Multithreaded Programming

Review what constitutes a thread Creating threads general Creating threads Java What happens if synchronization is not used? Assignment.

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Multiple Inheritance. Computer object can be viewed as

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

CST242 Concurrency Page 1

Threads in Java (Deitel & Deitel)

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

CS342: Software Design. November 21, 2017

Multithreaded Programming

Transcription:

CS 556 Distributed Systems Tutorial on 4 Oct 2002

Threads A thread is a lightweight process a single sequential flow of execution within a program Threads make possible the implementation of programs that seem to perform multiple tasks at the same time (e.g. multi-threaded Web servers) A new way to think about programming

We will cover: How to create threads in Java The Life Cycle of a thread Thread Priority Synchronization of threads Grouping of threads

How to create There are two ways to create a Java thread: 1. Extend the java.lang.thread class 2. Implement the java.lang.runnable interface

Extending the Thread class In order to create a new thread we may subclass java.lang.thread and customize what the thread does by overriding its empty run method. The run method is where the action of the thread takes place. The execution of a thread starts by calling the start method.

class MyThread extends Thread { private String name, msg; Example I public MyThread(String name, String msg) { this.name = name; this.msg = msg; public void run() { System.out.println(name + " starts its execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + msg); try { Thread.sleep(5000); catch (InterruptedException ie) { System.out.println(name + " finished execution");

Example I class MyThread extends Thread { private String name, msg; public MyThread(String name, String msg) { this.name = name; this.msg = msg; public void run() { System.out.println(name + " starts its execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + msg); try { Thread.sleep(5000); catch (InterruptedException ie) { System.out.println(name + " finished execution");

class MyThread extends Thread { private String name, msg; Example I public MyThread(String name, String msg) { this.name = name; this.msg = msg; public void run() { System.out.println(name + " starts its execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + msg); try { Thread.sleep(5000); catch (InterruptedException ie) { System.out.println(name + " finished execution");

Example I (cont.) public class test { public static void main(string[] args) { MyThread mt1 = new MyThread("thread1", "ping"); MyThread mt2 = new MyThread("thread2", "pong"); mt1.start(); mt2.start(); the threads will run in parallel

Example I (cont.) Typical output of the previous example: thread1 starts its execution thread1 says: ping thread2 starts its execution thread2 says: pong thread1 says: ping thread2 says: pong thread1 says: ping thread2 says: pong thread1 says: ping thread2 says: pong thread1 says: ping thread2 says: pong thread1 finished execution thread2 finished execution

Implementing the Runnable interface In order to create a new thread we may also provide a class that implements the java.lang.runnable interface Preffered way in case our class has to subclass some other class A Runnable object can be wrapped up into a Thread object Thread(Runnable target) Thread(Runnable target, String name) The thread s logic is included inside the run method of the runnable object

Example II class MyClass implements Runnable { private String name; private A sharedobj; public MyClass(String name, A sharedobj) { this.name = name; this.sharedobj = sharedobj; public void run() { System.out.println(name + " starts execution"); for (int i = 0; i < 5; i++) { System.out.println(name + " says: " + sharedobj.getvalue()); try { Thread.sleep(5000); catch (InterruptedException ie) { System.out.println(name + " finished execution");

Example II (cont.) class A { private String value; public A(String value) { this.value = value; public String getvalue() { return value; public class test2 { public static void main(string[] args) { A sharedobj = new A("some value"); Thread mt1 = new Thread(new MyClass("thread1", sharedobj)); Thread mt2 = new Thread(new MyClass("thread2", sharedobj)); mt1.start(); mt2.start(); shared variable

Example II (cont.) Typical output of the previous example: thread1 starts execution thread1 says: some value thread2 starts execution thread2 says: some value thread1 says: some value thread2 says: some value thread1 says: some value thread2 says: some value thread1 says: some value thread2 says: some value thread1 says: some value thread2 says: some value thread1 finished execution thread2 finished execution

We will cover: How to create threads in Java The Life Cycle of a thread Thread Priority Synchronization of threads Grouping of threads

The Life Cycle of a Thread

The Life Cycle of a Thread (cont.) The start method creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread's run method A thread becomes Not Runnable when one of these events occurs: Its sleep method is invoked. The thread calls the wait method. The thread is blocking on I/O A thread dies naturally when the run method exits

We will cover: How to create threads in Java The Life Cycle of a thread Thread Priority Synchronization of threads Grouping of threads

Thread Priority On a single CPU threads actually run one at a time in such a way as to provide an illusion of concurrency. Execution of multiple threads on a single CPU, in some order, is called scheduling. The Java runtime supports a very simple scheduling algorithm (fixed priority scheduling). This algorithm schedules threads based on their priority relative to other runnable threads.

Thread Priority (cont.) The runtime system chooses the runnable thread with the highest priority for execution If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. The chosen thread will run until: A higher priority thread becomes runnable. It yields, or its run method exits. On systems that support time-slicing, its time allotment has expired

Thread Priority (cont.) When a Java thread is created, it inherits its priority from the thread that created it. You can modify a thread's priority at any time after its creation using the setpriority method. Thread priorities are integers ranging between MIN_PRIORITY and MAX_PRIORITY (constants defined in the Thread class). The higher the integer, the higher the priority.

Thread Priority (cont.) Use priority only to affect scheduling policy for efficiency purposes. Do not rely on thread priority for algorithm correctness.

We will cover: How to create threads in Java The Life Cycle of a thread Thread Priority Synchronization of threads Grouping of threads

Synchronization of Threads In many cases concurrently running threads share data and must consider the state and activities of other threads. If two threads can both execute a method that modifies the state of an object then the method should be declared to be synchronized, allowing only one thread to execute the method at a time. If a class has at least one synchronized methods, each instance of it has a monitor. A monitor is an object that can block threads and notify them when it is available.

Synchronization of Threads (cont.) Example: public synchronized void updaterecord() { // critical code goes here Only one thread may be inside the body of this function. A second call will be blocked until the first call returns or wait() is called inside the synchronized method.

Synchronization of Threads (cont.) If you don t need to protect an entire method, you can synchronize on an object: public void foo() { // synchronized (this) { //critical code goes here // Declaring a method as synchronized is equivalent to synchronizing on this for all the method block.

Synchronization of Threads (cont.) The Object class has three synchronization methods: wait() notify() notifyall() These methods allow objects to wait until another object notifies them: synchronized( waitforthis ) { try { waitforthis.wait(); catch (InterruptedException ie) { To wait on an object, you must first synchronize on it.

Synchronization of Threads (cont.) A thread may call wait() inside a synchronized method. A timeout may be provided. If missing or zero then the thread waits until either notify() or notifyall() is called, otherwise until the timeout period expires. wait() is called by the thread owning the lock associated with a particular object (it causes the lock to be released). notify() or notifyall() are only called from a synchronized method. One or all waiting threads are notified, respectively. It s probably better/safer to use notifyall(). These methods don't release the lock. The threads awakened will not return from their wait() call immediately, but only when the thread that called notify() or notifyall() finally relinquishes ownership of the lock.

Synchronization of Threads (cont.) A Producer Consumer Problem public synchronized void put(int value) { while (available == true) { try { //wait for Consumer to get //value wait(); catch (InterruptedException e) { contents = value; available = true; //notify Consumer that value has //been set notifyall(); public synchronized int get() { while (available == false) { try { //wait for Producer to put //value wait(); catch (InterruptedException e) { available = false; //notify Producer that value has //been retrieved notifyall(); return contents;

Synchronization of Threads (cont.) The Thread class has a method, join(), which allows an object to wait until the thread terminates public void mymethod() { // Do some work here... // Can't proceed until another thread is done: otherthread.join(); // Continue work... Equivalent to waitfor() of java.lang.process

We will cover: How to create threads in Java The Life Cycle of a thread Thread Priority Synchronization of threads Grouping of threads

Grouping of Threads Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually. Java thread groups are implemented by the java.lang.threadgroup class When a Java application first starts up, the Java runtime system creates a ThreadGroup named main. Unless specified otherwise, all new threads that you create become members of the main thread group.

Grouping of Threads (cont.) To put a new thread in a thread group the group must be explicitly specified when the thread is created - public Thread(ThreadGroup group, Runnable runnable) - public Thread(ThreadGroup group, String name) - public Thread(ThreadGroup group, Runnable runnable, String name) A thread can not be moved to a new group after the thread has been created.

Grouping of Threads (cont.) A ThreadGroup may also contain other ThreadGroups allowing the creation of an hierarchy of threads and thread groups: public ThreadGroup(ThreadGroup parent, String name) To get the thread group of a thread the getthreadgroup of the Thread class should be called: ThreadGroup group = mythread.getthreadgroup();

Reference Read the Java Tutorial on Threads: http://java.sun.com/docs/books/tutorial/essential/threads/