Download Threads - Umar Kalim

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Advanced Java Programming
Umar Kalim
Dept. of Communication Systems Engineering
[email protected]
http://www.niit.edu.pk/~umarkalim
28/09/2007
Fall 2007
cs420
1
Agenda
Motivation for Threads
Threads
Thread Interruption
Synchronization & Locking
– Deadlocks
Interaction between threads
Thread Priorities
Fall 2007
cs420
Ref: CS193J Summer 2003 Stanford University
2
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
Fall 2007
cs420
3
Transistor Density
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 - dualcore 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!!!
Fall 2007
cs420
4
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 singlethreaded model code
– Programmers must start writing explicitly parallel code in
order to take benefit of the improvements in hardware!
Fall 2007
cs420
5
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
Fall 2007
cs420
6
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
Fall 2007
cs420
7
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
Fall 2007
cs420
8
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
Fall 2007
cs420
9
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
Fall 2007
cs420
10
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
Fall 2007
cs420
11
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
Fall 2007
cs420
12
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
Fall 2007
cs420
13
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
Fall 2007
cs420
14
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
Fall 2007
cs420
15
Creating Threads
Creating threads does not
mean that the thread will
start automatically
Fall 2007
cs420
16
Fall 2007
cs420
17
Daemon & User Threads
Daemon
~ Background
Threads
can only be ~ Providing Service
defined as daemon
threads before they are started
A thread created by a
daemon thread will
(by default) be a
daemon thread
Fall 2007
cs420
18
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()
Fall 2007
cs420
19
Connecting Threads
thread2.join()
– Suspend the current thread untill thread2
terminates
– Can throw InterruptedException
thread2.join(1000)
thread2.join(1000,500)
Fall 2007
cs420
20
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
Fall 2007
cs420
21
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
Fall 2007
cs420
22
Implementing Runnable Interface
Note that we can’t
setDaemon(true) in the
constructor since
JumbleNames is not
a subclass of Thread
Fall 2007
cs420
23
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?
Fall 2007
cs420
24
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
Fall 2007
cs420
25
Method Level Synchronization
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.
Only when the currently executing synchronized
method for an object has ended can another
synchronized method start for the same object
Guaranteed exclusive access!
Fall 2007
cs420
26
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
Fall 2007
cs420
27
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
Fall 2007
cs420
28
Using Synchronization
Example
Fall 2007
cs420
29
Bank
Fall 2007
cs420
30
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
Fall 2007
cs420
31
Fall 2007
cs420
32
Fall 2007
cs420
33
Fall 2007
cs420
34
Fall 2007
cs420
35
Fall 2007
cs420
36
What
went wrong?
Fall 2007
cs420
37
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.
Fall 2007
cs420
38
Deadlocks
Fall 2007
cs420
39
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
Fall 2007
cs420
40
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()
Fall 2007
cs420
41
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.
Fall 2007
cs420
42
Typical use
Fall 2007
cs420
43
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
Fall 2007
cs420
44
Summary &
Questions?
That’s all for today!
Fall 2007
cs420
45