Lecture10 Multithreading.

Slides:



Advertisements
Similar presentations
Concurrent Programming Abstraction & Java Threads
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.
1 Threads (Extra Lecture) Developing MultiThreaded Application (Part III) Overview  Waiting for Synchronized Data.  Creating Thread Groups.  Daemon.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
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.
Definitions Process – An executing program
Jan Java Threads Yangjun Chen Dept. Business Computing University of Winnipeg.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Lecture 4 : JAVA Thread Programming
Java Programming: Advanced Topics
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
10/10/20151 MULTITHREADED PROGRAMMING. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
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.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
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.
Threads Concurrency in Java. What is mult-tasking? Doing more than one task.
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
Threads.
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.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 18 Advanced Java Concepts Threads and 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
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Threading and Concurrency COM379T John Murray –
Packages Packages are containers for classes that are used to keep the class name space compartmentalized.. You can define classes inside a package that.
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.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
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.
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.
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.
© Andy Wellings, 2004 Thread Priorities I  Although priorities can be given to Java threads, they are only used as a guide to the underlying scheduler.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
Multithreading.
Multithreaded Programming in Java
Multithreading.
Java Based Techhnology
Multithreading.
Multithreaded Programming
Multithreading in java.
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Lecture10 Multithreading

Threads and Multithreading Thread Class Static Methods Objectives Threads and Multithreading Thread Class Static Methods Instance Methods Creating a Thread - extending the Thread class - implementing the Runnable interface Synchronization in Threads Thread Communication Deadlock

Concurrent Programming in Java Java supports concurrent programming via threads A thread is a single sequential flow of execution within a process A process is a self-contained running program with its own address space Multitasking OS can run more than one process at a time A processes can have multiple concurrently executing threads Threads in the same process share the same address space

A single thread is analogous to a sequential program beginning, execution sequence, end Threads, however, do not run on their own they run within a program

Multi-Threaded Programming Using multiple threads in the same program to perform more than one task at the same time

Multitasking & Multithreading Having more than one program working at what seems to be at the same time. The OS assigns the CPU to the different programs in a manner to give the impression of concurrency. There are two types of multitasking - preemptive and cooperative multitasking. Multithreading: Extends the idea of multitasking by allowing individual programs to have what appears to be multiple tasks. Each task within the program is called a thread.

Thread & Process 进程1 数据块 数据块 进程 程序段 程序段 线程1 . 线程N CPU 进程2 数据块 程序段 . . 线程 进程

Multi-Threads Concept Multiple threads on multiple CPUs Multiple threads sharing a single CPU

Some programs are required to do more than one thing at a time Advantages(1) Some programs are required to do more than one thing at a time These programs are easier to design and implement with threads Concurrency allows you to maintain a high availability of services each request for service can be handled by a new thread reduces bottleneck of pending requests

Concurrency can use CPU cycles more efficiently Advantages(2) Concurrency can use CPU cycles more efficiently if one thread becomes blocked, other threads can be run Concurrency supports asynchronous message passing objects can send messages and continue without having to wait for the message to be processed Concurrency supports parallelism on machines with multiple CPUs, concurrent programming can be used to exploit available computing power to improve performance

Threads can stop running for any number of reasons Limitations(1) Safety Since threads within a program share the same address space, they can interfere with one another Synchronization mechanisms are needed to control access to shared resources Liveness Threads can stop running for any number of reasons Deadlock can occur when threads depend upon each other to complete their activities

Multithreaded activities can be arbitrarily interleaved Limitations(2) Non-determinism Multithreaded activities can be arbitrarily interleaved no two executions of the program need be identical makes multithreaded programs harder to predict, understand and debug Thread Construction Overhead Constructing a thread and setting it in motion is slower and more memory intensive than constructing a normal object and invoking a method on it

Java Concurrency Support Java contains only a few basic constructs and classes to support concurrent programming Thread class and Runnable interface used to initiate and control threads ThreadGroup class Used to manage a group of threads synchronized and volatile keywords used to control code in objects that may participate in multiple threads wait, notify and notifyAll methods used to coordinate activities across threads States:new,runnable,blocked,dead No main loop, priorities

class CurrentThreadDemo{ public static void main(String args[]){ Example class CurrentThreadDemo{ public static void main(String args[]){ Thread t = Thread.currentThread(); t.setName(“My Thread”); System.out.println(“Current thread: ”+t); try { for (int n=5; n>0; n--) { System.out.println(“”+n); Thread.sleep(1000); // 1 second } } catch (InterruptedException e){ System.out.println(“Interrupted”); } // current thread: Thread[My Thread,5,main], 5,4,3,2,1 } // where 5 in [ ] is the default priority

The Thread class is part of the java.lang package. Using an object of this class, the corresponding thread can be stopped, paused, and resumed. There are many constructors and methods for this class, we will look at a few of them: Thread() Thread( String n) - creates a new Thread with the name n. Thread( Runnable target) - creates a new Thread object. Thread( Threadgroup group, Runnable target): this creates a new Thread object in the specified Threadgroup. Thread(Runnable target , String n) Thread (ThreadGroup group, Runnable target , String n) Thread(ThreadGroup group, String n)

instance methods: static methods: getPriority(); activeCount(); Methods in Thread Class static methods: activeCount(); currentThread(); sleep(); yield(); instance methods: getPriority(); setPriority(); start(); stop(); run(); isAtive(); suspend(); resume(); join();

Static Methods of Thread Class(1) static int activeCount() - returns the number of currently active threads. For example: num_ threads = Thread. activeCount(); static Thread currentThread() - returns the object corresponding to the currently executing thread (self reference) For example: Thread myself = Thread. currentThread(); static void sleep( long millis) throws InterruptedException this causes the current thread to sleep for the specified amount of time. Other threads will run at this time.

Static Methods of Thread Class(2) You can also specify the number of nanoseconds as well - static void sleep(long millis, int nano); static void yield() - causes the thread to yield the processor to any other waiting threads - Java does not guarantee preemption, you should use this to ensure fairness.

These methods control the thread represented by a thread object Instance Methods of Thread Class(1) These methods control the thread represented by a thread object int getPriority() - returns the threads priority - a value between Thread. MIN_ PRIORITY and Thread. MAX_ PRIORITY void setPriority( int newpri) - this sets the threads priority - high priority threads will preempt lower ones when they become ready to run. Thread myself = Thread. currentThread(); myself. setPriority( Thread. MAX_ PRIORITY);

- therefore the setPriority method may not succeed. Instance Methods of Thread Class(2) A ThreadGroup may restrict the maximum priority of all its member threads - therefore the setPriority method may not succeed. void start() throws IllegalThreadStateException – actually starts the thread - the thread starts and enters the run() method void stop() throws SecurityException - stops the thread void run() - this method is called when the thread is started. This is what the thread will execute while it is alive.

- returns a value indicating whether the thread is currently alive Instance Methods of Thread Class(3) boolean isAlive() - returns a value indicating whether the thread is currently alive - i.e. Started more recently and has not yet been died. void suspend() - suspends the threads execution void resume() - resumes the execution of the thread

The Runnable Interface To make a defined class runnable in a thread, we need to extend the class, as well as the Thread class. Impossible! public interface java.lang.Runnable { public abstract void run() // only one method }

Create Thread Objects(1) There are two ways to Thread objects creating objects from subclasses of the Java Thread class class ThreadX extends Thread { public void run() { //logic for the thread } Thread ThreadSubclass ThreadX tx = new ThreadX(); tx.start();

Create Thread Objects(2) implementing the Runnable interface for an object class RunnableY implements Runnable { public void run() { //logic for the thread } Runnable SomeSubclass implements RunnableY ry = new RunnableY(); Thread ty = new Thread(ry); ty.start();

class PingPong extends Thread { Constructing Threads (1) Extending the Thread class (a)Implement the run method; (b)Create an object(thread) of the class; (c) Start the thread class PingPong extends Thread { String word; int delay; PingPong(String whatToSay,int delayTime) { word=whatToSay; delay=delayTime; } public void run() { try { for(;;) { System.out.print(word + “ ”); sleep(delay); } } catch(InterruptedException e) { return; }} public static void main(String[] args) { new PingPong(“ping”,33).start(); // 1/30s new PingPong(“PONG”,100).start(); } // 1/10s } // Thread.run cannot throw exceptions, so must catch the exceptions of sleep pingPONGping pingpingPONG pingpingping PONGpingping pingPONG…

Constructing Threads (2) Implementing the Runnable interface (a)Implement the run method; (b)Create an object of the class; (c)Create a thread by the object; (d) Start the thread class RunPingPong implements Runnable{ String word; int delay; RunPingPong(String whatToSay,int delayTime){ word=whatToSay; delay=delayTime; } public void run(){ try{ for(::) { System.out.print(word+""); Thread.sleep(delay); } }catch(InterruptedException e){ return; } } public static void main(String{} args){ Runnable ping=new RunPingPong("ping",33); Runnable pong=new RunPingPong(“PONG",100); new Thread(ping).start(); new Thread(pong).start(); } }

Thread States(1) New: (a) When a thread is created by new, it’s not running(the new state). (b) Doing the book-keeping and determine the memory allocation are the tasks of start (before a thread can run.) Runnable: (a) Once you invoke the start method, the thread is runnable(may not yet be running). It’s up to the OS to get it time to run. (b) When the code inside the thread begins executing, the thread is running(also in the runnable state), ( c) Win95/NT give each runnable thread a slice of time to perform its task Blocked: (a) sleep, (b) suspend, (c) wait, (d) the thread calls an operation that is blocking on input/output, that is, an operation that will not return to its caller until input and output operations are complete

Thread States(2) Dead: (a) It dies a natural death because the run method exits, or it’s killed because someone invoked its stop method. (b) If a thread is stopped, it does not immediately die. The stop method throws an object(error, not exception) of type ThreadDeath to the thread object. Once the thread passes a ThreadDeath object to its thread base class

sleep interval expires State Transitions new start runnable dispatch (assign a processor) quantum expiration notify or notifyAll I/O completion running wait issue I/O request sleep suspend waiting blocked sleeping suspended stop complete sleep interval expires resume dead

Thread Control and Priorities Constants and Methods MAX- (10), MIN- (1) ,NORM-(5, default) public final void setPriority(int newPriority), newPriority must be between MIN- and MAX-, public final int getPriority() (2) Scheduling Pick the highest priority thread that is currently runnable It keeps running until either (a) yields, (b) ceases to be runnable(by dying or by entering the blocked state), (c ) replaced by a higher-priority thread that has become runnable(slept long enough, I/O operation complete, resume/notify called) Problem of same priority: round-robin: a thread is not scheduled again for execution until all other threads with the same priority have been scheduled at least once

Synchronization is a mechanism to control the the Synchronization in Threads Synchronization is a mechanism to control the the execution of different threads so that: when multiple threads access a shared variable, proper execution can be assured. Java has the synchronized keyword this can be used to identify a segment of code or method that should be accessible to just a single thread at a time. Before entering a synchronization region, a thread should obtain the semaphore associated with that region it is already taken, then the thread blocks (waits) until the semaphore is released.

…synchronized <retval><method_name>… Synchronized Methods …synchronized <retval><method_name>… An object is locked when a thread invokes a synchronized non-static method of the object. If another thread invokes the object’s synchronized methods, the thread is blocked before the object is unlocked If two or more threads modify an object, declare the methods that carry the modifications as synchronized Constructors needn’t be synchronized, because a new object is created in only one thread

Synchronized Statements synchronized <object_expr><statement > Example: public void run() { //caller中的run方法 synchronized (target) {target.call(msg);} } Use non-Synchronized Classes for multithreading extend the class,and redefine the methods as synchronized Use synchronized statements

Example class Callme { void call(String msg) { System.out.print(“[“+msg); try Thread.sleep(1000); catch (Exception e); System.out.println(“]”); } } class Caller implements Runnable{ String msg; Callme target; public Caller(Callme t, String s){ target=t; msg=s; new Thread(this).start(); } public void run(){ target.call(msg); } } //[1][2][3] expected class Synch { public static void main(String args[]) { Callme target = new Callme(); new Caller(target,”1”); new Caller(target,”2”); new Caller(target,”3”); } } Result: [1[2[3] ] void call(String msg) { synchronized void call(String msg) { Result: [1] [2] [3] Synchronized(target) { target.call(msg); }

Thread Communication A thread can temporarily release a lock so other threads can have an opportunity to execute a synchronized method. It is because the Object class defined three methods that allow threads to communicate with each other. void wait() - causes the thread to wait until notified - this method can only be called within a synchronized method void wait(long msec) throws InterruptedException void wait(long msec, int nsec) throws InterruptedException void notify() - notifies a randomly selected thread waiting for a lock on this object - can only be called within a synchronized method. void notifyall() - notifies all threads waiting for a lock on this object

Why : the producer-consumer example(1) class Q { int n; synchronized int get() { System.out.println(“Got:”+n); return n; } synchronized void put(int n) { this.n=n; System.out.println(“Put:”+n); } class Producer implements Runable{ Q q; Producer (Q q){ this.q=q; new Thread(this,”Producer”).start();} public void run(){ int i=0; while (true) { q.put(i++);} }

Why : the producer-consumer example(2) class Consumer implements Runable{ Q q; Consumer (Q q){ this.q=q; new Thread(this,” Consumer ”).start(); } public void run(){ int i=0; while (true) { q.get();} } } Put:1 Got:1 Got 1 Put:2 Put:3 Put:4… class PC{ public static void main(String args[]) { Q q= new Q(); new Producer(q); new Consumer(q); }

Why : the producer-consumer example(3) A solution: polling class Q { int n; boolean valueSet = false; synchronized int get() { while (!valueSet) ; System.out.println(“Got:”+n); valueSet=false; return n; } synchronized void put(int n) { while (valueSet) ; this.n=n; valueSet=true; System.out.println(“Put:”+n); } } Problem: CPU(very slow) Solution: wait, notify, notifyAll(final methods of Object)

public final void wait (…) throws InterruptedException Parameters: (long timeout) // unit: ms (long timeout, int nanos) () = (0) // nanos: 0..999999(ns) Wait for being notified, or for a given time. Wait(0) :wait until notified synchronized void doWhenCondition(){ while (!cond) wait(); …..//do something when cond is true } When the thread is suspended by ‘wait’, it will free the lock on the object

Notify public final void notify()/notifyAll() ‘notify’: to notify one thread which is waiting for some condition. (used when you know the exact thread) ‘notifyAll’: to notify all threads waiting for some conditions Whenever a method changes the state of an object to change, it should call notify(All). That gives the waiting threads a chance to see if circumstances have changed The Corrected Example Note: use if ( not while): only one producer and one consumer(more quickly). Wait and notify(All) are usually used in sync. methods

Why : the producer-consumer example(3) class Q { int n; boolean valueSet = false; synchronized int get() { if (!valueSet) try {wait();} catch(InterruptedException e) {} System.out.println(“Got:”+n); valueSet=false; notify(); return n; } synchronized void put(int n) { if (valueSet) try {wait();} catch(InterruptedException e){} this.n=n; valueSet=true; System.out.println(“Put:”+n); notify(); } } 返回

Example For one queue, multiple threads use put and get class Queue{ Element head,tail; public synchronized void append(Element p){ if (tail==null) head=p; else tail.next=p; p.next=null; tail=p; notify(); } public synchronized Element get(){ try{ while(head==null) wait(); }catch(InterruptedException e){ return null;} Element p=head; head=head.next; if (head==null) tail=null; return p; } }

Deadlock Deadlock is an error that can be encountered in multithreads. It occurs when two or more threads wait indefinitely for each other to relinquish locks. - Assume that thread-1 holds a lock on object-1 and waits for a lock on object-2. Thread-2 holds a lock on object-2 and waits for a lock on object-2. - Neither of these threads may proceed. Each waits forever for the other to relinquish the lock it needs.

Example(1-1) T1: A a T2: a1(); a2(); b b1(); B2(); B

Example(1-2) class A { B b; synchronized void a1() { System.out.println(“Starting a1”); b.b2(); } synchronized void a2() { System.out.println(“Starting a2”); class B { A a; synchronized void b1() { System.out.println(“Starting b1”); a.a2(); } synchronized void b2() { System.out.println(“Starting b2”);

Example(1-3) class Thread1 extends Thread { A a; Thread1(A a) { this.a = a; } public void run() { for (int i = 0; i < 100000; i++) a.a1(); class Thread2 extends Thread { B b; Thread2(B b) { this.b = b; } public void run() { for (int i = 0; i < 100000; i++) b.b1();

Example(1-4) public class DeadlockDemo { public static void main(String args[]) { //Create objects A a = new A(); B b = new B(); a.b = b; b.a = a; Thread1 t1 = new Thread1(a); //Create threads Thread2 t2 = new Thread2(b); t1.start(); t2.start();} try {t1.join(); t2.join();} //wait for threads to complete catch (Exception e) { e.printStackTrace(); } System.out.println(“Done!”); }

Daemon A daemon is simply a thread that has other role in life than to serve others When only daemon threads remain, Java exits Daemon Methods public final boolean isDaemon() public final void setDaemon(boolean on) daemon/user If the thread is active, IllegalThreadStateException is throwed. So it must be called before the thread started

Thread Groups Categorize threads by functionality, improve security A thread belongs to a certain group Thread groups may be defined in thread constructors Default: A new thread is put into the group of the thread creating it When a thread is dead, it’s deleted from the group A thread group may contain other group( hierarchy) A thread can access other threads in the same group or in the sub-groups

Constructors ThreadGroup(String groupname) create a new group, which is a sub-group of the group of current thread If groupname is null: NullPointerException (2) ThreadGroup(ThreadGroup parent, String name) create a new group, which is a sub-group of the parent If can not create a thread in the given group: SecurityExc

Some Methods int activeCount() number of threads in this group and all of its sub-groups if activeCount()==0, no thread of this group is runnable (2) void stop() kill all threads in a thread group (3) int enumerate(Thread list[]) gets references to every active thread in this thread group Use activeCount() to get an upper bound for the array (4) ThreadGroup getParent() (5) ThreadGroup getThreadGroup() (6) void resume()/suspend() resume/suspend all threads in this group and all of its child groups

Homework 设计一个银行应用的多线程程序。该程序能通过各自单独的线程完成对同一个银行帐号进行存款和取款: • BankAccount:帐号类,包括同步方法:存款和取款(取款时钱不足则等待存钱金额满足取款金额) • Saver:存款线程 • Spender:取款线程 • Banking:主程序,完成对某个帐号的存、取款