Lesson 9 Intro to Java Threads. What are threads?  Like several concurrent subprograms running within the same address space.  Within a program, individual.

Slides:



Advertisements
Similar presentations
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Advertisements

Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Definitions Process – An executing program
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
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.
Tutorial 2 Adventures in Threading presented by: Antonio Maiorano Paul Di Marco.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
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.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Java Threads Representation and Management of Data on the Internet.
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.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
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.
1 Web Based Programming Section 8 James King 12 August 2003.
Java Threads and higher level constructs for doing concurrent programming.
Threading Eriq Muhammad Adams J
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
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.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
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 in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
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.
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.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Multithreading.
Multithreaded Programming in Java
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
21 Threads.
Multithreading in java.
NETWORK PROGRAMMING CNET 441
Threads and Multithreading
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Lesson 9 Intro to Java Threads

What are threads?  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 – shared memory model

Why use threads?  Single-user programs/clients –continuously responsive user interfaces e.g., accept input when event handler is busy e.g., make help menu accessible during time- consuming database operation, etc. Speed up tasks when multiple processors available  Servers –Allows multiple client connections simultaneously

General examples  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.

Concrete example  Consider two versions of a program which animates a ball bouncing around in a window.  In one version, the animation takes place in the event handler thread. Thus, the gui is frozen for the whole animation  In a second version, the animation takes place in a new thread spawned in the event handler. This gives the event thread a chance to operate.  Compare Bounce.java and BounceThread.java

Second concrete example  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

Machinery – How to setup threads in Java

How to create separate threads in Java -- technique I  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).

How to create threads Technique 2  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.

Simple Example Thread 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 …”); }

Example using second technique 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 …”); }

What happens to new threads?  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.

Thread States  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

Thread states, cont. –Dead Dies a natural death because the run method exits normally Dies abruptly after an uncaught exception terminates the run method –Use isAlive() method to determine if Thread is currently alive (either runnable or blocked).

Thread Priority  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 Priority  Each Java thread is given a numeric priority between MIN_PRIORITY and MAX_PRIORITY.  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.

Thread Priority  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.

Thread Priority  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.

A common scenario: polling vs callbacks  How do we implement the following? –Thread1 spawns Thread2 –Thread1 does work –Thread2 does work that results in new value of variable (or new data in file, etc). –Thread1 finishes work and needs updated value (or file) from Thread2’s work. –How can we synchronize activities?

What’s so difficult? Atomic processes, sharing resources, synchronization, deadlock

Bottom Line  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 …

Managing Threads  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 …

Producer/Consumer example  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?

Producer Class 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) { } }}}

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

Consumer class 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); }}}

Comments  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.

How to synchronize code  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

Synchronizing a method  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.

Synchronizing a block  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.

Synchronizing a block, cont. –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.

wait() and notify()/notifyAll()  A common scenario is as follows: A thread enters a synchronized block of code (and thus obtains the object lock). The code cannot continue the sychronized block until some other thread has done some work on the same object and left it in a new state Thus, the first thread wants to temporarily relinquish the lock to another thread.

wait() and notify()/notifyAll()  Called when a thread needs to wait for some other thread(s) to complete a task before continuing.  The current thread simply calls wait(), and the thread freezes until another object calls notify() upon the waiting object (or notifyAll()).  notifyAll() is much safer since notify() chooses randomly!

wait() and notify()/notifyAll()  Beware, wait() can only be called from a synchronized method or block of code.  When wait is called on an object, its lock is released until it is notified.

Moving out of blocked state  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.

Synchronized Cubbyhole 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; }

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

Interrupting Threads  Thread terminates when run method ends.  So, run method should check once in a while whether there is more work to do in regular way.  However, what if thread is sleeping or otherwise blocked?  This is where interrupt() method comes into play.

Interrupting, cont.  When interrupt method is called on thread that is blocking, the blocking call is terminated by an InterruptException. public void run(){ try{ while (more work to do){ do work } catch(InterrupetedException e){ //thread was interrupted during sleep or wait //do whatever you wish here }

Interrupting, cont.  Problem with previous code is that interrupts only succeed if thread is blocked in call to sleep() or wait().  Can get around this by calling interrupted() periodically to see if thread has recently been interrupted: while (!interrupted() && more work to do){ // do work }

Deadlock – Dining Philosophers example  ntial/threads/deadlock.html

Atomic operations -- Bank Account example  See BankTest.java and SynchBankTest.java

Tips on writing thread-safe code  Obvious way is to use synchronization (see next slide) –Performance problems –Can lead to deadlock –General rule: don’t overuse if possible  Local variables –Each thread gets its own copy  “Immutable sequences” –Objects which can’t be changed (String, Integer, etc.)

Re-entrant threads  Java’s model supports a concept called re- entrant threads.  If a thread t obtains a lock on object o by calling a synchronized method in o (say m1), and then from within m1 calls a second sychronized method in o m2, deadlock is guaranteed not to occur.

Other useful methods  join() tells the calling thread to halt execution until the second thread (whose yield method is called), has completed. Thread t = new Thread(runnableObj); t.join();

Additional Topics  Priorities  Thread Groups  Creating Thread pools  Threads and Swing  Higher-level methods – Timer class

Distributed Objects  Higher level alternative to sockets  RMI or CORBA