Download presentation
Presentation is loading. Please wait.
1
Lecture10 Multithreading
2
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
3
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
4
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
5
Multi-Threaded Programming
Using multiple threads in the same program to perform more than one task at the same time
6
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.
7
Thread & Process 进程1 数据块 数据块 进程 程序段 程序段 线程1 . 线程N CPU 进程2 数据块 程序段 . . 线程 进程
8
Multi-Threads Concept
Multiple threads on multiple CPUs Multiple threads sharing a single CPU
9
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
10
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
11
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
12
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
13
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
14
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
15
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)
16
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();
17
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.
18
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.
19
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);
20
- 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.
21
- 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
22
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 }
23
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();
24
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();
25
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…
26
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(); } }
27
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
28
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
29
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
30
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
31
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.
32
…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
33
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
34
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); }
35
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
36
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++);} }
37
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); }
38
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)
39
public final void wait (…) throws InterruptedException
Parameters: (long timeout) // unit: ms (long timeout, int nanos) () = (0) // nanos: (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
40
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
41
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(); } } 返回
42
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; } }
43
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.
44
Example(1-1) T1: A a T2: a1(); a2(); b b1(); B2(); B
45
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”);
46
Example(1-3) class Thread1 extends Thread { A a; Thread1(A a) { this.a = a; } public void run() { for (int i = 0; i < ; i++) a.a1(); class Thread2 extends Thread { B b; Thread2(B b) { this.b = b; } public void run() { for (int i = 0; i < ; i++) b.b1();
47
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!”); }
48
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
49
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
50
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
51
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
52
Homework 设计一个银行应用的多线程程序。该程序能通过各自单独的线程完成对同一个银行帐号进行存款和取款: • BankAccount:帐号类,包括同步方法:存款和取款(取款时钱不足则等待存钱金额满足取款金额) • Saver:存款线程 • Spender:取款线程 • Banking:主程序,完成对某个帐号的存、取款
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.