Threads in Java a tutorial introduction (revision 7) Christian Ratliff Senior Technology Architect Core Libraries.

Slides:



Advertisements
Similar presentations

Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
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.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Java Programming: Advanced Topics
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
1 Web Based Programming Section 8 James King 12 August 2003.
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.
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.
SurfaceView.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
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.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
1 OS Review Processes and Threads Chi Zhang
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.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
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.
Asynchronous Programming Writing Asynchronous Code in Java SoftUni Team Technical Trainers Software University
Java Thread Programming
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Multi Threading.
Multithreading.
Background on the need for Synchronization
Multithreaded Programming in Java
Operating System (013022) Dr. H. Iwidat
Computer Engg, IIT(BHU)
Lecture 21 Concurrency Introduction
Multithreading.
Synchronization Issues
Threading And Parallel Programming Constructs
Java Based Techhnology
Multithreading.
Concurrency: Mutual Exclusion and Process Synchronization
Threads and Multithreading
Representation and Management of Data on the Internet
Foundations and Definitions
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threads in Java a tutorial introduction (revision 7) Christian Ratliff Senior Technology Architect Core Libraries Group, DeLorme 28 August 2002

Copyright © 2002, DeLorme Demo SerialPrime.java

What is a ‘thread’? Free Online Dictionary of Computing (FOLDOC). Sharing a single CPU between multiple tasks (or "threads") in a way designed to minimize the time required to switch tasks. This is accomplished by sharing as much as possible of the program execution environment between the different tasks so that very little state needs to be saved and restored when changing tasks.

Copyright © 2002, DeLorme Wherefore art thou? ► To maintain responsiveness of an application during a long running task. ► To enable cancellation of separable tasks. ► Some problems are intrinsically parallel. ► To monitor status of some resource (DB). ► Some APIs and systems demand it: Swing. ► To take advantage of multiple processors. ► It looks great on your resume.

Copyright © 2002, DeLorme Threads and Processes CPU Process 1Process 3Process 2Process 4 main run GC

Copyright © 2002, DeLorme Oh No! Computer Science!! ► Some critical concepts:  non-determinism  race condition  concurrency  parallel scheduling ► At least I am not charging $10,000 a year!

Copyright © 2002, DeLorme Concurrency vs. Parallelism CPUCPU1CPU2

Copyright © 2002, DeLorme Concurrency vs. Parallelism CPU1CPU2 main run CPU main run main run main run main RAM this.count

Copyright © 2002, DeLorme Whatever! How about some code? ► In Java, thread instances come in two varieties:  java.lang.Runnable  java.lang.Thread

Copyright © 2002, DeLorme java.lang.Runnable ► An adapter which enables any given class to operate as a thread. ► Requires one method be implemented:   public void run() ► Most common method for adding threads to an application. ► Prevents certain thread operations (e.g. ). ► Prevents certain thread operations (e.g. Thread.isAlive() ).

Copyright © 2002, DeLorme Demo counter/CounterThread0.java

java.lang.Thread ► A base class with maximum threading functionality. ► While less common, it is far more powerful. ► Forces the focus of your class to its “threadedness”. ► Minimum requirements are like Runnable:   public void run()

Copyright © 2002, DeLorme Demo counter/CounterThread1.java

Alive Thread State Diagram New ThreadDead Thread Running Runnable new CounterThread1(max); run() method returns while (…) { … } Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor cntThread.start();

Copyright © 2002, DeLorme Demo counter/CounterThread2.java

Cooperative Multithreading ► By adding a Thread.yield() call to the subthread and the main, all 50 values were fetched. ► Success depends on careful cooperation between each thread. ► This is not a safe option. ► Wizards: What is the other problem here?

Copyright © 2002, DeLorme Accessing Shared Resources ► The only safe mechanism is through locking. ► There are many kinds of locks:  busy-wait-flag, semaphore, mutex, etc ► They can offer many differing semantics:  barriers, reader-writer, critical sections, etc ► Java offers one, coherent mechanism:  monitors ► Wizards: Who invented the monitor? When?

Copyright © 2002, DeLorme synchronized ► Monitors are implemented on a per-object basis. ► Each method on an object which accesses protected resources is marked synchronized. ► The monitor is a “fence” around the object. ► Each synchronized method is a “gate” in that fence.

Copyright © 2002, DeLorme Inside Monitors PrimeCache isPrime addPrimeToCache main run primesmaxPrime

Copyright © 2002, DeLorme Inside Monitors PrimeCache isPrime addPrimeToCache main run primesmaxPrime

Copyright © 2002, DeLorme Inside Monitors PrimeCache isPrime addPrimeToCache main primesmaxPrime

Copyright © 2002, DeLorme Inside Monitors PrimeCache isPrime addPrimeToCache main primesmaxPrime

Copyright © 2002, DeLorme Inside Monitors PrimeCache isPrime addPrimeToCache primesmaxPrime

Copyright © 2002, DeLorme Demo sieve/SieveThread0.java

Other uses of synchronized ► There are two additional situations where synchronized may be used:  The synchronized statement may be used in a method to synchronize an arbitrary block of code.  A static method may be labeled as synchronized, in which case the monitor it attached to the enclosing class.

Copyright © 2002, DeLorme *SNORE* WAKE UP! SNACK TIME!

Signalling ► Use of the synchronized attribute alone is not sufficient. ► If a thread must wait for both access to a resource and a condition to be satisfied, the only obvious option is a busy-wait. ► There must be a better way!

Copyright © 2002, DeLorme Signalling Object.wait() ► Gives up ownership of the monitor. ► Blocks until timeout, interruption, or notification. ► On waking, the thread enters the monitor acquisition phase. Object.notify() Object.notifyAll() ► Does not give up ownership of the monitor. ► Wakes an arbitrary, or all, thread(s) blocked on the monitor. ► No thread preference!

Copyright © 2002, DeLorme Demo sieve/SieveThread1.java

Exception Handling ► When an exception is emitted out of the run() method, the thread is terminated. ► The exception is consumed by the JVM because once Thread.start() is called, the created thread is split from the caller. ► Java provides a means for dispatching a copy of the exception: ThreadGroup.

Copyright © 2002, DeLorme java.lang.ThreadGroup ► When an exception is emitted by the Thread.run method, the method ThreadGroup.uncaughtException is called. ► Derive a class from ThreadGroup. ► Override the uncaughtException method, relaying the thread and exception information.

Copyright © 2002, DeLorme Demo group/SieveThread.java

Jane, stop this crazy thing! ► There are three ways to halt a thread:  The wrong way ► Thread.suspend(), Thread.stop(), Thread.destroy()  The long way ► The thread tests a control flag in the instance  The pull-the-rug-out way ► Thread.interrupt(), destroy a dependent resource

Copyright © 2002, DeLorme Demo halt/SieveThread.java

Three Concurrency Risks ► Atomicity  An atomic operation permits no interruptions.  The JMM promises that 32bit reads and writes are atomic.  Any types greater than 32bits in size should be protected with a monitor.  No multi-step operation can ever be atomic without the use of a monitor.  Fields marked as volatile are always completely flushed at every write (even 64bit ones).

Copyright © 2002, DeLorme Three Concurrency Risks ► Ordering  In a synchronized block, instructions are not reordered (as-if-serial). This is the same as within try-catch-finally blocks.  When another thread is watching the fields of the synchronized block, it perceives non- reordered semantics as well.

Copyright © 2002, DeLorme Three Concurrency Risks class Test { static int i = 0; static int j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } class Test { static int i = 0; static int j = 0; static synchronized void one() { i++; j++; } static synchronized void two() { System.out.println("i=" + i + " j=" + j); }

Copyright © 2002, DeLorme Three Concurrency Risks class Test { static int i = 0; static int j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } class Test { static volatile int i = 0; static volatile int j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); }

Copyright © 2002, DeLorme Three Concurrency Risks ► Visibility  Reads and writes of instance fields must act on the same data, even on different CPUs.  Exiting a monitor flushs all writes from the variable cache to main memory.  Acquiring a monitor forces the JVM to reload any cached variable information.  When a thread exits, its cache is flushed to memory.

Copyright © 2002, DeLorme Advanced Topics ► Thread scheduling issues: one-to-one, many-to-one, many-to-many. ► Thread pooling implementations. ► Deadlock detection and prevention.