Download presentation
Presentation is loading. Please wait.
1
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering umar.kalim@niit.edu.pk http://www.niit.edu.pk/~umarkalim 28/09/2007
2
Fall 2007cs4202 Agenda Motivation for Threads Threads Thread Interruption Synchronization & Locking –Deadlocks Interaction between threads Thread Priorities Ref: CS193J Summer 2003 Stanford University
3
Fall 2007cs4203 Faster Computers Why are computers today faster than 10 year ago? –Process improvements Chips are smaller and run faster –Superscalar pipelining parallelism techniques Doing more than one thing at a time from the one instruction stream Instruction Level Parallelism (ILP) –There is a limit to the amount of parallelism that can be extracted from a single instruction stream About 3x to 4x We are well within the diminishing returns region here
4
Fall 2007cs4204 Transistor Density –2001 – P4 – 0.18μm ~ 42 Million –Dual-core Conroe utilizes just 298 million transistors ~ 65nm technology –Penryn - 45nm technology ~ 410 million transistors - dual- core model, and 820 million transistors for the quad-core variants What can we use them for? –More cache –More functional units Instruction Level Parallelism –Multiple threads!!!
5
Fall 2007cs4205 Hardware vs. Software Writing single threaded software is easier –Therefore we have used hardware to drive the performance of software Hardware is however hitting a limit –Not on the number of transistors yet But on how much parallelism it can use based on single- threaded model code –Programmers must start writing explicitly parallel code in order to take benefit of the improvements in hardware!
6
Fall 2007cs4206 Software concurrency Processes –Concurrency ~ multitasking –The ability to run multiple applications at once Example: Unix processes launched from a shell, piped to another process –Separate address space –Cooperate using read/write streams (pipes) –Synchronization is easy Since there is no shared address space
7
Fall 2007cs4207 Threads The ability to do multiple things at once within the same application –Finer granularity of concurrency Lightweight –Easy to create and destroy Shared address space –Can share memory variables directly –May require more complex synchronization logic because of shared address space
8
Fall 2007cs4208 Advantages of threads… Use multiple processors –Code is partitioned in order to be able to use n processors at once This is not easy to do! But Moore’s Law may force us in this direction Hide network/disk latency –While one thread is waiting for something, run the others –Dramatic improvements even with a single CPU Need to efficiently block the connections that are waiting, while doing useful work with the data that has arrived –Writing good network codes relies on concurrency! Keeping the GUI responsive –Separate worker threads from GUI thread
9
Fall 2007cs4209 Why Concurrency is a Hard Problem No language construct to alleviate the problem –Memory management can be solved by a garbage collector, no analog for concurrency Counter-intuitive –Concurrency bugs are hard to spot in the code –Difficult to get into the concurrency mindset No fixed programmer recipe either Client may need to know the internal model to use it correctly –Hard to pass the Clueless-Client test Concurrency bugs are random –Show up rarely, often not deterministic/reproducible easily –Rule of thumb: if something bizarre happens try and note the current state as well as possible
10
Fall 2007cs42010 Java Threads Java includes built-in support for threading! VM transparently maps threads in Java to OS threads –Allows threads in Java to take advantage of hardware and operating system level advancements –Keeps track of threads and schedules them to get CPU time –Scheduling may be pre-emptive or cooperative
11
Fall 2007cs42011 Current Running Thread “Thread of control” or “Running thread” –The thread which is currently executing some statements A thread of execution –Executing statements, sending messages –Has its own stack, separate from other threads A message send sends the current running thread over to execute the code in the receiver
12
Fall 2007cs42012 Java Thread class A Thread is just another object in Java –It has an address, responds to messages etc. –Class Thread in the default java.lang package A Thread object in Java is a token which represents a thread of control in the VM –We send messages to the Thread object; the VM interprets these messages and does the appropriate operations on the underlying threads in the OS
13
Fall 2007cs42013 Creating Threads in Java Two approaches –Subclassing Thread Subclass java.lang.Thread Override the run() method –Implementing Runnable Implement the runnable interface Provide an implementation for the run() method Pass the runnable object into the constructor of a newThread Object
14
Fall 2007cs42014 Why two approaches? Remember: Java supports only single- inheritance –If you need to extend another class, then cannot extend thread at the same time Must use the Runnable pattern Two are equivalent –Whether you subclass Thread or implement Runnable, the resulting thread is the same –Runnable pattern just gives more flexibility
15
Fall 2007cs42015 Thread Lifecycle Steps in the lifecycle of a thread –Instantiate new Thread Object (thread) Subclass of Thread Thread with a runnable object passed in to constructor –Call thread.start() This begins execution of the run() method –Thread finishes or exits when it exits the run() method Idiom – run() method will have some form of loop in it! –Optional - thread.sleep or thread.yield() –Thread.stop(), thread.suspend() and thread.resume() are deprecated! See http://java.sun.com/j2se/1.4.1/docs/guide/misc/threadPrimitiveDe precation.html
16
Fall 2007cs42016 Creating Threads Creating threads does not mean that the thread will start automatically
17
Fall 2007cs42017
18
Fall 2007cs42018 Daemon & User Threads Daemon ~ Background ~ Providing Service Threads can only be defined as daemon threads before they are started A thread created by a daemon thread will (by default) be a daemon thread
19
Fall 2007cs42019 Stopping a thread interrupt() –Set a flag –Must be checked within the run() method sleep() & InterruptedException –Flag is cleared after exception is thrown isInterrupted() –Only checks whether the flag is set or not? –The flag may be set, while the thread is still running; not obliged to terminate –Tests only, does not change the value –Unlike interrupted() which tests the flag and clears if it is set Use isAlive()
20
Fall 2007cs42020 Connecting Threads thread2.join() –Suspend the current thread untill thread2 terminates –Can throw InterruptedException thread2.join(1000) thread2.join(1000,500)
21
Fall 2007cs42021 Thread Scheduling If OS provides pre-emptive multi-tasking, then sleep may not be required –Remember sleep() is used along with interrupt(), thus if there is no sleep(), no need for the InterruptedException try catch block
22
Fall 2007cs42022 Thread Scheduling yield() –Give other threads a chance to execute if they are waiting and –Do not want to wait for a specific duration i.e. if no threads are waiting, current thread resumes immediately
23
Fall 2007cs42023 Implementing Runnable Interface Note that we can’t setDaemon(true) in the constructor since JumbleNames is not a subclass of Thread
24
Fall 2007cs42024 Managing Threads Threads just don’t start randomly and execute till they exit run() –Undesirable behaviour Example ~ ATM access –The bank clerk checks the balance of the customer’s account, which is $500. –The ATM asks for the account balance. –The clerk adds the value of the check, $100, to the account balance to give a figure of $600. –The ATM takes $50 off the balance of $500, which gives a figure of $450, and spits out 5 $10 bills. –The clerk assigns the value of $600 to the account balance. –The ATM assigns the value $450 to the account balance. What went wrong?
25
Fall 2007cs42025 Synchronization To ensure that when several threads want access to a single resource, only one thread can access it at any given time –Method level –Block level Every Object has an associated lock
26
Fall 2007cs42026 Method Level Synchronization Only when the currently executing synchronized method for an object has ended can another synchronized method start for the same object Guaranteed exclusive access! Note that there’s no constraint here on simultaneously executing synchronized methods for two different objects of the same class. It’s only concurrent access to any one object that is controlled.
27
Fall 2007cs42027 Instance methods and threads There is a distinction between an object that has instance methods that you declared as synchronized in the class definition and the threads of execution that might use them
28
Fall 2007cs42028 Class locks Synchronization to static methods in a class, –Only one of those static methods in the class can be executing at any point in time; –This is per-class synchronization –The class lock is independent of any locks for objects of the class
29
Fall 2007cs42029 Using Synchronization Example
30
Fall 2007cs42030 Bank
31
Fall 2007cs42031 Classes A Bank class to represent the bank computer An Account class to represent the account at the bank A Transaction class to represent a transaction on the account—a debit or a credit, for example A Clerk class to represent a bank clerk A class containing the method main() that will start the process
32
Fall 2007cs42032
33
Fall 2007cs42033
34
Fall 2007cs42034
35
Fall 2007cs42035
36
Fall 2007cs42036
37
Fall 2007cs42037 What went wrong?
38
Fall 2007cs42038 Synchronizing Statement Blocks No other statements or statement blocks in the program that are synchronized on the object theObject can execute while this statement is executing. –This applies even when the statement is a call to a method, which may in turn call other methods.
39
Fall 2007cs42039 Deadlocks
40
Fall 2007cs42040 Communication b/w threads Combination of synchronization and sleep –Potential Deadlocks Can be avoided? The Object class defines the methods wait(), notify(), and notifyAll() Can only be called from within a synchronized block or method –IllegalMonitorStateException
41
Fall 2007cs42041 wait(), wait(long timeout), wait(long timeout, int nanos) 3 overloaded versions Suspends the current thread until the notify() or notifyAll() method is called for the object to which the wait() method belongs. When any version of wait() is called, the thread releases the synchronization lock it has on the object –Any other method or code block synchronized on the same object can execute A thread can call wait() on the an object even if another thread had done so earlier –So is the case with notify () and notifyAll()
42
Fall 2007cs42042 notify(), notifyAll() notifyAll() –This restarts all threads that have called wait() for the object to which the notifyAll() method belongs. notify() –This restarts a thread that has called the wait() method for the object to which the notify() method belongs. –If several threads have called wait() for the object, you have no control over which thread is notified –Thus it is better to use notifyAll(). If no threads are waiting, the method does nothing.
43
Fall 2007cs42043 Typical use
44
Fall 2007cs42044 Thread Priorities object.setPriority(Thread.MIN_PRIORITY); // low priority = 1 object.setPriority(Thread.MAX_PRIORITY); // high priority = 10 object.setPriority(Thread.NORM_PRIORITY); // normal priority = 5 IllegalArgumentException
45
Fall 2007cs42045 Summary & Questions? That’s all for today!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.