Lecture10 Multithreading.

51
10-1 Programming in Java Multithreadi ng Lecture10

description

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

Transcript of Lecture10 Multithreading.

Page 1: Lecture10 Multithreading.

10-1

Programming in Java

Multithreading

Lecture10

Page 2: Lecture10 Multithreading.

10-2

Programming in Java

Objectives

• Threads and Multithreading• Thread Class

Static Methods Instance Methods Creating a Thread

- extending the Thread class- implementing the Runnable i

nterface• Synchronization in Threads• Thread Communication• Deadlock

Page 3: Lecture10 Multithreading.

10-3

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

Concurrent Programming in Java

Page 4: Lecture10 Multithreading.

10-4

Programming in Java

Thread

• 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

Page 5: Lecture10 Multithreading.

10-5

Programming in Java

Multi-Threaded

• Multi-Threaded Programming– Using multiple threads in the same program to

perform more than one task at the same time

Page 6: Lecture10 Multithreading.

10-6

Programming in Java

Multitasking & Multithreading

• Multitasking: 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.

Page 7: Lecture10 Multithreading.

10-7

Programming in Java

进程 1数据块程序段

进程 2数据块程序段

.

.

.

.

.

.

数据块程序段线程 1

.

.

.线程 N

程序段进程 线程

CPU

进程

Thread & Process

Page 8: Lecture10 Multithreading.

10-8

Programming in Java

Multi-Threads Concept

Multiple threads on multiple CPUs

Multiple threads sharing a single CPU

Page 9: Lecture10 Multithreading.

10-9

Programming in Java

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

Page 10: Lecture10 Multithreading.

10-10

Programming in Java

• 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

Advantages(2)

Page 11: Lecture10 Multithreading.

10-11

Programming in Java

• Safety– Since threads within a program share the same a

ddress space, they can interfere with one another– Synchronization mechanisms are needed to contr

ol 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

Limitations(1)

Page 12: Lecture10 Multithreading.

10-12

Programming in Java

• 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

Limitations(2)

Page 13: Lecture10 Multithreading.

10-13

Programming in Java

Java Concurrency Support• Java contains only a few basic constructs and clas

ses 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

Page 14: Lecture10 Multithreading.

10-14

Programming in Java

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

Example

Page 15: Lecture10 Multithreading.

10-15

Programming in Java

• 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): t

his 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)

Thread Class

Page 16: Lecture10 Multithreading.

10-16

Programming in Java

Methods in Thread Class

static methods:activeCount();currentThread();sleep();yield();

instance methods:getPriority();setPriority();start();stop();run();isAtive();suspend();resume();join();

Page 17: Lecture10 Multithreading.

10-17

Programming in Java

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.

Page 18: Lecture10 Multithreading.

10-18

Programming in Java

• You can also specify the number of nanoseconds as well- static void sleep(long millis, int nano);

Static Methods of Thread Class(2)

• 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.

Page 19: Lecture10 Multithreading.

10-19

Programming in Java

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 andThread. 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);

Page 20: Lecture10 Multithreading.

10-20

Programming in Java

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.

Instance Methods of Thread Class(2)

Page 21: Lecture10 Multithreading.

10-21

Programming in Java

• boolean isAlive() - returns a value indicating whether the thread is cu

rrently alive - i.e. Started more recently and has not yet been die

d. • void suspend() - suspends the threads execution• void resume() - resumes the execution of the thread

Instance Methods of Thread Class(3)

Page 22: Lecture10 Multithreading.

10-22

Programming in Java

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 }

Page 23: Lecture10 Multithreading.

10-23

Programming in Java

• There are two ways to Thread objects creating objects from subclasses of

the Java Thread class

Thread

ThreadSubclass

class ThreadX extends Thread {public void run() {//logic for the thread}

}

ThreadX tx = new ThreadX();tx.start();

Create Thread Objects(1)

Page 24: Lecture10 Multithreading.

10-24

Programming in Java

class RunnableY implements Runnable {public void run() {//logic for the thread}

}

RunnableY ry = new RunnableY();Thread ty = new Thread(ry);ty.start();

Runnable

SomeSubclass

implements

Create Thread Objects(2) implementing the Runnable interface for an object

Page 25: Lecture10 Multithreading.

10-25

Programming in Java

Constructing Threads (1)

(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

pingPONGping

pingpingPONG

pingpingPONG

pingpingping

PONGpingping

PONGpingping

pingPONG…

Page 26: Lecture10 Multithreading.

10-26

Programming in Java

(2) Implementing the Runnable interface (a)Implement the run method; (b)Create an object of the cla

ss; (c)Create a thread by the object; (d) Start the thread

Constructing Threads (2)

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(); } }

Page 27: Lecture10 Multithreading.

10-27

Programming in Java

• 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(1)

Page 28: Lecture10 Multithreading.

10-28

Programming in Java

• 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

Thread States(2)

Page 29: Lecture10 Multithreading.

10-29

Programming in Java

new

start

runnable

running

waiting sleeping suspended blocked

dead

dispatch (assign a processor)quantum

expiration I/O completion

resumestop

waitsleep suspend

complete

issue I/O request

sleep interval expires

notify or notifyAll

State Transitions

Page 30: Lecture10 Multithreading.

10-30

Programming in Java

(1) 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()

Thread Control and Priorities

(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

Page 31: Lecture10 Multithreading.

10-31

Programming in Java

Synchronization in Threads• Synchronization is a mechanism to

control the theexecution 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.

Page 32: Lecture10 Multithreading.

10-32

Programming in Java

• …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 Methods

Page 33: Lecture10 Multithreading.

10-33

Programming in Java

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

Synchronized Statements

Page 34: Lecture10 Multithreading.

10-34

Programming in Java

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]

]

]

synchronized void call(String msg) {

Result: [1] [2] [3]

Synchronized(target) { target.call(msg); }

Example

void call(String msg) {

Page 35: Lecture10 Multithreading.

10-35

Programming in Java

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 - can only be called within a synchronized method.

Page 36: Lecture10 Multithreading.

10-36

Programming in Java

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++);} } }

Page 37: Lecture10 Multithreading.

10-37

Programming in Java

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();} } }class PC{ public static void main(String args[]) { Q q= new Q(); new Producer(q); new Consumer(q); }}

Put:1 Got:1 Got 1 Got:1 Got:1 Got:1Put:2Put:3Put:4…

Why : the producer-consumer example(2)

Page 38: Lecture10 Multithreading.

10-38

Programming in Java

• 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)

Why : the producer-consumer example(3)

Page 39: Lecture10 Multithreading.

10-39

Programming in Java

•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

Wait

Page 40: Lecture10 Multithreading.

10-40

Programming in Java

• 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

Notify

Page 41: Lecture10 Multithreading.

10-42

Programming in Java

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

Example •For one queue, multiple threads use put and get

Page 42: Lecture10 Multithreading.

10-43

Programming in Java

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.

Page 43: Lecture10 Multithreading.

10-44

Programming in Java

T1:T1: AA

a1();a1();

a2();a2();

bb BB

aa

b1();b1();

B2();B2();

T2:T2:

Example(1-1)

Page 44: Lecture10 Multithreading.

10-45

Programming in Java

class A {B b;synchronized void a1() { System.out.println(“Starting a1”);b.b2();}synchronized void a2() { System.out.println(“Starting a2”);}}

Example(1-2)

class B {A a;synchronized void b1() {System.out.println(“Starting b1”);a.a2();}synchronized void b2() {System.out.println(“Starting b2”);}}

Page 45: Lecture10 Multithreading.

10-46

Programming in Java

class Thread1 extends Thread {A a;Thread1(A a) {this.a = a;}public void run() {for (int i = 0; i < 100000; i++)a.a1();}}

Example(1-3)

class Thread2 extends Thread {B b;Thread2(B b) {this.b = b;}public void run() {for (int i = 0; i < 100000; i++)b.b1();}}

Page 46: Lecture10 Multithreading.

10-47

Programming in Java

public class DeadlockDemo {public static void main(String args[]) { //Create objectsA 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 completecatch (Exception e) { e.printStackTrace(); }System.out.println(“Done!”); }}

Example(1-4)

Page 47: Lecture10 Multithreading.

10-48

Programming in Java

• 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

Daemon

Page 48: Lecture10 Multithreading.

10-49

Programming in Java

• 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

Thread Groups

Page 49: Lecture10 Multithreading.

10-50

Programming in Java

(1) 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

Constructors

Page 50: Lecture10 Multithreading.

10-51

Programming in Java

(1) int activeCount() • number of threads in this group and all of its sub-groups • if activeCount()==0, no thread of this group is runnable

(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

(2) void stop()• kill all threads in a thread group

(4) ThreadGroup getParent()(5) ThreadGroup getThreadGroup()(6) void resume()/suspend()• resume/suspend all threads in this group and all of its child groups

Some Methods

Page 51: Lecture10 Multithreading.

10-52

Programming in Java

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

Homework