Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
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.
1 Java threads: synchronization. 2 Thread states 1.New: created with the new operator (not yet started) 2.Runnable: either running or ready to run 3.Blocked:
Effective Java: Concurrency Last Updated: Fall 2011.
Lecture8 – Ch 5 Higher-level concurrency patterns Synchronized collections Concurrent collections Producer-consumer pattern  Serial thread confinement.
CSC Multiprocessor Programming, Spring, 2011 Outline for Chapter 5 – Building Blocks – Library Classes, Dr. Dale E. Parson, week 5.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Wrapup CS221 – 4/29/09. Concurrent Collections Java supplies a set of concurrent collections you can use manage sets of data in a multi- threaded.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
1 CS2200 Software Development Lecture: Multi-threading II A. O’Riordan, 2009.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Week 9 Building blocks.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
May 14, 2002Serguei A. Mokhov, 1 Kickstart Intro to Java Part II COMP346/ Operating Systems Revision 1.5 July 23, 2003.
Threads some important concepts Simon Lynch
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
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.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
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.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Java Threads 1 1 Threading and Concurrent Programming in Java Queues D.W. Denbo.
Internet Software Development Controlling Threads Paul J Krause.
Synchronizing threads, thread pools, etc.
Kernel Locking Techniques by Robert Love presented by Scott Price.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Polytechnic University of Tirana Faculty of Information Technology Computer Engineering Department A MULTITHREADED SEARCH ENGINE AND TESTING OF MULTITHREADED.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
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.
…in java DThread Pools x. Thread Pools: Why? Source code updates – copy/paste of run/runnable method for each thread is exhausting There is overhead to.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Advanced Programming Concurrency and Threads Advanced Programming. All slides copyright: Chetan Arora.
Java.util.concurrent package. concurrency utilities packages provide a powerful, extensible framework of high-performance threading utilities such as.
Asynchronous Programming Writing Asynchronous Code in Java SoftUni Team Technical Trainers Software University
A brief intro to: Parallelism, Threads, and Concurrency
Thread Pools (Worker Queues) cs
A Scheme concurrency library
COT 4600 Operating Systems Fall 2009
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Threads Chate Patanothai.
Multithreading Chapter 23.
some important concepts
Presentation transcript:

Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD

Introduction References include: –McLaughlin and Flanagan, 2004, Java 1.5 Tiger: A Developer’s Notebook, O’Reilly –Flanagan, 2005, Java in a Nutshell, 5 th edition, O’Reilly –Oaks and Wong, 2004, Java Threads, 3 rd edition, O’Reilly

What was added for Java 5? 1.Some new thread utilities to support pools of threads and thread scheduling. 2.Explicit locks and condition variables for synchronisation between threads. 3.Atomic classes to allow developers to avoid explicit synchronisation while writing concurrent applications.

Concurrency Utilities In the past, if a thread terminated by throwing an exception, the exception went to an internal class called a ThreadGroup, which usually did something inappropriate. To avoid this, you had to extend ThreadGroup, which was a lot of arcane coding. Now you can define an exception handler for the Thread to handle the problem.

Thread-safe Collections Java 1.4’s collection classes are generally not thread-safe. If you want to a collection class with threads, you had to use Hashtable or Vector, both of which are old. Java 5 has thread-safe collection classes in java.util.concurrent.*. These include: –ConcurrentHashMap –CopyOnWriteArrayList –CopyOnWriteArraySet –ConcurrentLinkedQueue

BlockingQueues Java 5 provides queues for use as first-in-first-out data structures. If these run out of space, you have problems, but you can use java.util.concurrent.BlockingQueue to get around the problem. There are a number of specialised BlockingQueues that you can use. If a BlockingQueue is full, the put() method will wait for room. If it is empty, a take() will wait for data.

Separating Thread Logic from Executor Logic Instead of passing a Runnable object to a Thread and scheduling things in detail, you can pass it to an Executor, and have the Executor handle the threading logic. This can also be used to handle call-backs. The Callable interface indicates that the object can be called. It consists of a call() method. You can pass a Callable object to a FutureTask, give the task to a Thread, start the thread, automatically wait, and get the results some time later using a get call on the FutureTask.

Advanced Synchronising You have four specialised synchroniser classes: –Semaphore –CountDownLatch –Exchanger –CyclicBarrier These allow you to complex synchronisation such as is done in operating systems running on multiple CPUs.

Atomic Types An atomic operation is one that is indivisible— guaranteed to complete without being interrupted by another thread. Security operations need to be atomic, for example. In java.util.concurrent.atomic.*, there are a number of atomic types that allow you to perform atomic operations on primitive types. These include get(), set(), getAndSet(), and compareAndSet().

Lock and Condition Extends the synchronized keyword. This allows you to lock variables and wait on those variables becoming unlocked. Other locks are supported including ReadWriteLock.