Java Threads and higher level constructs for doing concurrent programming.

Slides:



Advertisements
Similar presentations
Concurrent Programming Abstraction & Java Threads
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Lesson 9 Intro to Java Threads. What are threads?  Like several concurrent subprograms running within the same address space.  Within a program, individual.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
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.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
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.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
Threading Eriq Muhammad Adams J
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
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.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
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.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Multithreading (Based on Chapter 29 (Multithreading) Liang book and Chapter 5 (Threads) Elliotte Rusty book) Dr. Tatiana Balikhina 1.
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.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
Multithreading.
Multithreaded Programming in Java
Lecture 21 Concurrency Introduction
Threads Chate Patanothai.
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Threads Chapter 4.
Multithreading in java.
Threads and Multithreading
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Threads and higher level constructs for doing concurrent programming

Background How to create/run threads interrupt(), join() Thread priorities/states Synchronized blocks of code wait(),notify()/notifyAll

 Like several concurrent subprograms running within the same address space.  Within a program, individual threads are explicitly “spawned” and give the appearance of simultaneously running sequences of commands.  On a single processor machine, the simultaneous running is an illusion – cpu is time splicing.  Differ from separate processes in that each process runs in its own address space – threads are a lightweight shared memory model

 Single-user programs/clients  continuously responsive user interfaces  e.g., accept input when event handler is busy  Do not block on time-consuming i/o or socket operations  e.g., make help menu accessible during time- consuming database operation, etc.  Speed up tasks when multiple processors available  Model multiple clients on single platform  Servers  Allows multiple clients to connect without “busy signal”

 User clicks GUI button to download web page (occurs in separate thread so GUI isn’t “frozen”)  Massive numerical problems split among processors  assumes each thread runs on separate processor; not necessarily the case  Server spawns thread for each client connection and main thread goes back to accept()  User clicks button which begins time- consuming database lookup. Client can accept more input while lookup is taking place.

 Imagine a GUI program that performs a time- consuming task in the event handler How can the GUI remain responsive?  If we do task in a separate thread and sleep it periodically, user interface thread will appear “live”.  See FrameThread.java and FrameNoThread.java

 Extend Thread. Specifically...  Create a class that extends Thread and place the work that the Thread will carry out in the run() method (ie override the run method).  Create an object from your Thread class.  Call the start() method on the Thread object.  The new Thread then enters the runnable state (it may or may not run immediately depending on resources/priority).

 Implement Runnable. Specifically...  Create a class that implements the Runnable interface. Place all of the work that the Thread will perform in the run() method.  Create an object from your Runnable class.  Create a Thread object and pass the Runnable object to the constructor.  Call the start() method on Thread object.  See simple examples under basic directory.

Class ThreadExample{ public static void main(String[] args){ System.out.println(“Main thread started”); MyFirstThread t = new MyFirstThread(); t.start(); System.out.println(“main thread continuing”); } Class MyFirstThread extends Thread{ void run(){ System.out.println(“in new thread …”); }

class ThreadTest{ public static void main(String[] args){ System.out.println(“main thread started …”); MyRunnableObj r = new MyRunnableObj(); Thread t = new Thread(r); t.start(); System.out.println(“Main thread continuing”); } Class MyRunnableObj implements Runnable{ public void run(){ System.out.println(“new thread started …”); }

Common these days, like with other libraries, to use anonymous inner classes for simple threads Thread t = new Thread(new Runnable(){ public void run(){ //do work here } }); This provides a few advantages: -Reduces proliferation of small, simple classes -Allows t access to instance variables of outer class -Puts code definition close to where object is created.

 Main thread continues  New threads execute the run method and die when they are finished  If any thread calls System.exit(0), it will kill all threads.  Think of each run() as its own main  Program does not exit until all non-daemon threads die.

 Four states a Thread can be in:  New  When you create with new operator but haven’t run yet.  Runnable  When you invoke start() method. Note that Thread is not necessarily running, could be waiting.  Blocked  When sleep() is called  Blocking operation such as input/output  wait() is called by the Thread object  Thread tries to obtain a lock on a locked object

 Dead  Dies a natural death because the run method exits normally  Dies abruptly after an uncaught exception terminates the run method  As of 1.5 Java supports a getState() method in the Thread class that reports one of these states.

 The execution of multiple threads on a single CPU is called scheduling.  The Java runtime supports a very simple, deterministic scheduling algorithm known as fixed priority scheduling.  Thread.setPriority(int newPriority)  Value must be between MIN_PRIORITY and MAX_PRIORITY  NORM_PRIORITY is normal value

 When multiple threads are ready to be executed, the thread with the highest priority is chosen for execution.  Only when that thread stops, or is suspended for some reason, will a lower priority thread start executing.  Scheduling of the CPU is fully preemptive. If a thread with a higher priority than the currently executing thread needs to execute, the higher priority thread is immediately scheduled.

 The Java runtime will not preempt the currently running thread for another thread of the same priority. In other words, the Java runtime does not time-slice. However, the system implementation of threads underlying the Java Thread class may support time-slicing. Do not write code that relies on time-slicing.  In addition, a given thread may, at any time, give up its right to execute by calling the yield method. Threads can only yield the CPU to other threads of the same priority--attempts to yield to a lower priority thread are ignored.  static void Thread.yield()

 When all the runnable threads in the system have the same priority, the scheduler chooses the next thread to run in a simple, non- preemptive, round-robin scheduling order.

Atomic processes, sharing resources, synchronization/coordination, avoiding deadlock

 Any time a writeable variable is visible to more than one thread, potential problems exist.  Simple example: two clients try to purchase item at same time.  Order or execution unpredictable  If (itemsLeft > itemsRequested) not reliable!  Must create “thread-safe” programs  More on this later …

 Everything in either Object or Thread class  Two classes of methods:  Those defined in Object  wait(), notify(), notifyAll()  Those defined in Thread class  join(), sleep(), interrupt(), isAlive(), yield(), etc.  All involve situations where threads communicate with each other in some way.  Will discuss later …

 It is best to learn how these methods work with a few canonical examples  First is the following scenario  A client issues a command that requires a time consuming operation – in this case we can imagine it to be reading a lot of data from an input stream  The UI should not block during this operation. Other things can be done that are useful. Thus, we place the operation in a thread.  However, certain commands require this read operation to be complete before they can sensibly be executed.  How do we say “block this command until the other thread is finished”?  Study ExJoin.java in class notes … it uses Thread.join() to handle this situation.

 In the next example we imagine a thread that is launched into a time consuming operation – in this case an expensive calculation in a large loop.  The main thread remains responsive to the GUI and reports on progress of the worker thread. As this progress is monitored, the client may wish to terminate the worker thread.  How can this be done?  We use the following Thread methods to carry this out:  void interrupt(): sends an interrupt request to thread  boolean isInterrupted(): tests weather the thread is interrupted  static Thread currentThread(): returns object represent this Thread.

 Thread.interrupt() is an important method. It supersedes stop() and suspend(), which are now deprecated.  The idea with interrupt() is that one thread notifies another thread that it might be time to stop, but leaves it up to the thread itself to decide.  How does a thread know that it has been interrupted?  By calling isInterruped() or, if the thread is blocking  An InterruptedException is thrown

 The next and probably most important topic is atomicity – how to ensure that one thread completes an entire sequence of instructions without being preempted by another thread.  Note that instructions are not lines of Java code – one line can be made up of several instructions, possibly many more  The motivation is simple – if one thread takes on action best on a test condition, but another thread begins to execute and halts the original thread, the condition may no longer be true by the time the initial thread recommences execution.  How do we “lock” threads out of a piece of code until one thread is finished executing?

 One thread is called the Producer. Producer shoves consecutive integers into a Cubbyhole object as fast as it can.  Other thread is called Consumer. Consumer grabs from the Cubbyhole object as fast as it can.  Consumer would like to grab once for each put by the Producer, but what if one goes faster than the other?  We want to block Consumer from consuming until Producer has finished producing.  Likewise, we want to block Producer from producing again until Consumer has conumed.

public class CubbyHole{ private int contents; public synchronized int get() { return contents; } public synchronized void put(int value) { contents = value; }

public class Producer extends Thread { private CubbyHole cubbyhole; private int number; public Producer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { for (int i = 0; i < 10; i++) { cubbyhole.put(i); System.out.println("Producer #" + this.number + " put: " + i); try { sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } }}}

public class Consumer extends Thread { private CubbyHole cubbyhole; private int number; public Consumer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { int value = 0; for (int i = 0; i < 10; i++) { value = cubbyhole.get(); System.out.println("Consumer #" + this.number + " got: " + value); }}}

 Note that these classes of themselves do not preclude a race condition.  This is done by shychronizing access to the Cubbyhole object.  We want to guarantee that the Consumer thread can’t get until the Producer has produced.  Need to study wait() and notify() methods.

 void wait(): relinquishes the object lock without exiting the object method – ie if the thread can only proceed within object method once another thread has changed the object’s state. Can only be called if the calling thread already possesses the lock – otherwise, an IllegalMonitorStateException is thrown. The lock is given to the next waiting thread, or chosen randomly if more than one thread is waiting on the same object’s lock.  void notify()/notifyAll(): a thread cannot pull itself out of the wait() state, but requires another thread to call notify/notifyAll to allow it to reclaim the lock and continue.

 This topic confuses many beginning Java programmers  Two forms:  synchronized(objReference){ …}//synchronize a block  synchronized void methodName(){…}//synch a method  Former is more general, but causes confusion. Best to use simple form whenever possible.  Second form is equivalent to:  synchronized(this) for entire method body  Recently java has added a java.util.concurrent.locks package, which contains much of the same functionality but perhaps packaged in a more friendly way. We will go over these briefly as the last step in this lecture.

 Fairly straightforward rules:  When a given thread is executing a synchronized method in an object, no other thread can execute an other synchronized method on the SAME OBJECT!  We say that the first thread obtains a lock on the object and doesn’t release it until it finishes the synched method  Beware that only code which tries to obtain a lock on the object will have to wait. For example, if a thread wishes to call a non-synched method in the object, it will not be blocked.

 Remember the following rules:  When a thread encounters a synchronized block of code, it attempts to obtain a lock on the object that is being synchronized upon.  Consider the first thread in a program that encounters the lock. Since it is the first thread, it will successfully obtain the lock.  Now consider a second thread that encounters any synchronized block of code that synchronzies on the same object.

 This second thread will attempt to obtain the lock on objReference.  It will fail to obtain the lock unless the first thread has released it. This means that the first thread must have finished its synchronized block of code.  If the second thread cannot obtain the lock, it must wait until the first thread releases it.

 Must use the opposite route that put the Thread into the blocked state  If put to sleep, specified time interval must elapse.  If waiting for i/o operation, operation must have finished.  If the thread called wait(), then another thread must call notify/notifyAll.  If waiting for a lock, then owning thread must have relinquished the lock.

public class CubbyHole { private int contents; private boolean available = false; public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; }

public synchronized void put(int value) { while (available == true) { try { wait(); } catch (InterruptedException e) { } } contents = value; available = true; notifyAll(); }

 This time we have a queue of specified length containing String messages.  Two Consumer and one Producer thread run simultaneously.  The Producer adds messages to the queue up to the maximum queue size, then waits for the Consumer to remove one before proceeding.  The Consumer removes messages from the queue unless none are present, in which case it waits for the Producer to add one.  Note that this concept of a blocking queue mimics a very common model where a resource may not be available and polling is inelegant and inefficient. notify() replaces polling with a pushback model.

 The term “race condition” is used to describe a situation where the correctness of multi- threaded code depends on an ordering of operations that is not guaranteed in any execution.  If one thread “races” ahead of another, the code could fail to give correct results.  A very nice example is the concept of a Bank with clients simultaneously moving money within and between accounts.

 An Account object allows clients to withdraw, deposit, and see the balance of a single account.  One thread, the Saver, deposits money.  Another thread, the Spender, withdraws money.  In this example, if there are insufficient funds to allow a specified withdrawal, then, rather than block, an Exception is thrown.  Study the example and see what can go wrong..

 In this more realistic example many Account objects are created and threads are create to transfer money between them in different random ways.  If the code is correct, the total amount of money should not change – it is just moved around but never withdrawn.  However, this requires proper atomicity of the transfer method. This example shows what can go wrong and how to fix it.