Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.

Slides:



Advertisements
Similar presentations
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
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.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
COMP1681 / SE15 Introduction to Programming
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fundamentals of Python: From First Programs Through Data Structures
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.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Java Programming: Advanced Topics
Threads some important concepts Simon Lynch
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.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Web Based Programming Section 8 James King 12 August 2003.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
OPERATING SYSTEM SUPPORT DISTRIBUTED SYSTEMS CHAPTER 6 Lawrence Heyman July 8, 2002.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Laboratory - 4.  Threading Concept  Threading in.NET  Multi-Threaded Socket  Example.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Department of Computer Science and Software Engineering
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
Multithreaded Programming in Java David Meredith Aalborg University.
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 in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
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.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Threaded Programming in Python
PA1 Discussion.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Distributed Systems - Comp 655
Multithreading Chapter 23.
Multithreading.
Threaded Programming in Python
21 Threads.
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Threads and Multithreading
Multithreaded Programming in Java
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Thread per client and Java NIO
Presentation transcript:

Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3

SE15 Fast-Track: Multithreaded JavaFT4–2 Today’s Learning Objectives For you to see how Java programs can be made multithreaded through the use of Thread objects & the Runnable interface For you to appreciate the problems that can occur when threads interfere with one another… …and for you to understand how to fix those problems

SE15 Fast-Track: Multithreaded JavaFT4–3 Session Outline What are threads? Thread class vs. Runnable interface Network servers revisited Dealing with race conditions Scheduled tasks

SE15 Fast-Track: Multithreaded JavaFT4–4 What Are Threads? A multitasking OS time-slices between processes to give the illusion of concurrency Threads are a finer-grained concept allowing concurrent tasks within a single process Much easier to share data between threads, because they operate in the same address space

SE15 Fast-Track: Multithreaded JavaFT4–5 Threads in Java Basic features in java.lang Thread class Runnable interface Additional features in java.util Timer & TimerTask (since J2SE 1.3) Advanced features (J2SE 5.0) Thread pools Thread-aware collections Locks, semaphores, etc

SE15 Fast-Track: Multithreaded JavaFT4–6 Thread vs. Runnable Extend Thread class Override run method with code that needs to be executed in a separate thread Can’t do this if the code is in a class that already has a superclass (and it may not make sense, anyway…) Implement Runnable interface Forces us to write a run method, containing code that needs to be executed in a separate thread Need to create separate Thread objects to operate on our Runnable objects

SE15 Fast-Track: Multithreaded JavaFT4–7 Writing Servers Revisited Socket alone isn’t sufficient to write a server We need something to ‘sit by the phone, waiting for incoming calls’… Java provides ServerSocket class to Listen on a particular port Negotiate connection with client Open a Socket connection between hosts Spawn thread to handle communication with client

SE15 Fast-Track: Multithreaded JavaFT4–8 Race Conditions What happens if two threads have access to the same object and each tries to modify object state? Outcome depends on how the instructions executed by the two threads are interleaved Example: bank transfers aload_0 getfield #16 iload_1 dup2 laload iload_3 i2l lsub lastore accounts[from] -= amount; accounts[to] += amount;

SE15 Fast-Track: Multithreaded JavaFT4–9 Synchronization Methods that should not be interrupted can be tagged with synchronized modifier When a synchronized method is called by a thread, a mutex lock on the object is acquired The lock prevents other threads from calling any synchronized method on that object… …until the first thread exits its synchronized method

SE15 Fast-Track: Multithreaded JavaFT4–10 wait & notify wait method allows a thread to wait within a synchronized method and relinquish the object lock Waiting thread goes onto ‘wait list’ and will be ignored by thread scheduler For a thread to be removed from wait list, another thread must invoke notify or notifyAll on same object notifyAll method unblocks all waiting threads, freeing them to compete for the object lock after current thread has exited the synchronized method

SE15 Fast-Track: Multithreaded JavaFT4–11 Scheduled Tasks Implement task to be scheduled as a class that extends abstract class TimerTask Override run method with code be executed Timer class has methods to execute TimerTask code at a later time, once only or repeatedly Scheduled tasks are placed in an ordered queue and are executed sequentially by a single thread Tasks should be short-lived

SE15 Fast-Track: Multithreaded JavaFT4–12 Summary We have Seen how Java provides the Thread class and Runnable interface to support multithreaded code Considered how a simple network server can be made multithreaded Examined how race conditions can arise with threads, and how synchronization can help resolve problems Looked at how tasks can be scheduled to run at regular intervals in Java programs