Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.

Slides:



Advertisements
Similar presentations
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Multithreading The objectives of this chapter are:
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Slides 8c-1 Programming with Shared Memory Java Threads and Synchronization Review The following notes are based upon the Java tutorial at
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
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.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Java Programming: Advanced Topics
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Threads some important concepts Simon Lynch
1 Java Threads and Synchronization Review Modified from slides taken from
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
1 Web Based Programming Section 8 James King 12 August 2003.
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
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.
Threading and Concurrency COM379T John Murray –
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.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Multi-Threading in Java
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.
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.
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.
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:
Multithreading / Concurrency
Multi Threading.
Chapter 19 Java Never Ends
Threads Chate Patanothai.
Multithreading.
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Java Concurrency 17-Jan-19.
Java Concurrency.
Java Concurrency.
Threads and Multithreading
Java Concurrency 29-May-19.
Multithreading The objectives of this chapter are:
some important concepts
Presentation transcript:

Concurrency and Thread Yoshi

Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing interface Runnable – Solve the disability of multiple inheritance

Implement Runnable There is only one public abstract method defined in interface Runnable – public void run() A runnable object can be passed to the constructor of class Thread Check the source code of public void run() in class Thread! – What is the strategy? private Runnable target; … public void run() { if (target != null) target.run(); }

Thread States

Thread.sleep() A static method – Why? Note that the obtained locks are not released!

join The join method allows one thread to wait for the completion of another. – t.join(); The current thread pauses execution until t's thread terminates. Note that join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify.

Thread Safe Example: Dinning Philosopher Problem hers_problem hers_problem

Lock / key In Java, every class and every instance of a class has a lock associated with it The synchronized keyword identifies places where a thread must acquire the lock before proceeding Note that: For those methods which do not have synchronized, they are not constrained, so they can access anything – The logical correctness should be guaranteed by you!

Keyword: Synchronized synchronized(fork1) { synchronized(fork2) { //eat }

Deadlock It is possible that every philosopher gets only one fork, and no one can continue – Deadlock happens Breaking the circular waiting – Release the lock you obtained before fork1.wait(), fork2.wait() – Once you call something.wait(), you are queued into the waiting list, and need to be notified by someone later something.notify(); something.notifyAll();

Two Ways for Synchronization synchronized void method() { //… } //When will we use this? void method() { synchronized(this) { //… }

Wait and Notify What are they waiting for?

Related Data Structures java.util.ArrayList – Thread unsafe java.util.Vector – Thread safe

Synchronization in static method Race condition also happens in static method Remember that we always require some method/block to obtain the lock to continue

Review this synchronized void method() { //… } void method() { synchronized(this) { //… }

In static method synchronized static void method() { //… } class XY { static void method() { synchronized(XY.class) { //… }

Blueprints are also objects Hard Disk XY.class ClassLoader An instance of class Class (Blueprint of XY) XY instance 1 newInstance() XY instance 2

Actually, we can also use an additional lock class XY { static Object lock = new Object(); static void method() { synchronized(lock) { //… }

Be Careful When You Create Threads class XY extends Thread { int x = 4; public void run() { //modify x } XY obj1 = new XY(); XY obj2 = new XY(); class XY implements Runnable { int x = 4; public void run() { //modify x } XY xyObj = new XY(); Thread obj1 = new Thread(xyObj); Thread obj2 = new Thread(xyObj); What’s the difference?

Atomic Access An atomic action is one that effectively happens all at once An atomic action cannot stop in the middle – It either happens completely, or it doesn't happen at all The following are guaranteed – Reads and writes are atomic for reference variables and for most primitive variables All types except long and double – Reads and writes are atomic for all variables declared volatile Including long and double variables

Immutable Objects in Multi-Thread An object is considered immutable if its state cannot change after it is constructed – What’s the benefit?

References Sun Java Tutorial: Concurrency – /concurrency/index.html /concurrency/index.html Java Multithread Design Pattern – Java 多執行緒與平行處理 Java 多執行緒與平行處理