08 Threads
08 Threads
Multi-Threaded t0
Programming
P1
in JAVA t2
t1
P0
t1
t0
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
In the Beginning…
• Computers ran a single task at a time…
• Punch cards were placed into a feeder.
• The cards were then read, compiled and run.
• Batch processing extended this…
• Groups of punch cards could be run one after the next.
• This increases return of investment in the hardware.
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
1
Then Came the Operating System
• The OS sits between programs and the hardware.
• Contributions include:
• Uniform interaction between hardware
• I/O abstractions (e.g., filesystems)
• Standardized interaction libraries (e.g., libc)
• Multi-user Capabilities
• Memory management and protection (virtual address space)
• Scheduling and time sharing
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
2
Multi-Tasking
• K users share the hardware running N tasks
• Tasks are time-sliced quickly to give the illusion
that all tasks are running in parallel
• Each task thinks it’s the only one on the machine
• Cooperative Multi-Tasking: (Win3.1, MacOS)
• Each process yield the processor when it feels fit
• Pre-Emptive Multi-Tasking: (UNIX)
• The OS scheduler decides who should run when
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Multi-Threading
• If each user can run many tasks…
• Why can’t each task have many “sub-tasks”?
• This is usually called multi-threading.
• Threads are like “lightweight” tasks…
• Scheduling for execution is pretty much the same
• Differences include:
• They share the same memory space.
• They may not have as much OS overhead.
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
3
Why do we want threads?
• Why multi-user / multi-tasking?
• Processor is idle less, people can share a computer.
• Better return on hardware investment
• We use threads for somewhat similar reasons:
• Make sure processors are fully utilized
• Don’t block on I/O
• True parallel execution on multiprocessor hardware
• Other cool things
• Games: intelligent user agents, animation
• Automatic garbage collection, hidden from the user
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
4
Programming Threads in JAVA
• Two ways it can be done
• Create a class, extend Thread
• Override the run() method
• Instantiate the class
• Call start()
• Create a class, implement Runnable
• Implement the run() method
• Instantiate your class
• Instantiate a Thread class, pass your class in constructor
• Call start()
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
5
TwoThreadsTest Output
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
6
The Listener
public class Listener {
public static void main(String[] args) {
ServerSocket srvSock = new ServerSocket(4567);
while (keepRunning) {
// when we get a connection, spawn off a
// thread to handle it… this means we can
// keep listening for other connections
// while the first client is serviced
Socket conn = srvSock.accept();
(new Thread(new sockHandler(conn))).start();
}
}
}
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
The Handler
public class sockHandler implements Runnable {
private Socket conn = null;
public sockHandler(Socket conn) {
this.conn = conn;
}
public void run() {
InputStreamReader ISR = new
InputStreamReader(conn.getInputStream());
BufferedReader fromClient = new
BufferedReader(ISR);
OutputStreamReader OSR = new
OutputStreamReader(conn.getOutputStream());
PrintWriter toClient = new
PrintWriter(OSR);
// DO CLIENT SERVICING HERE
}
}
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
7
Data Parallel Programming
• We spawn off many threads to estimate PI
• As each thread completes, we update our estimate
• If we were running on MP hardware with a
Hotspot JVM, these threads would run on
separate processors and harness true parallelism
• Notice that the threads share a single memory
space… that’s why we can communicate between
the sub-tasks and controller without RMI
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
8
PI Estimation Control Program
public class EstimatePi {
private double pi = 0.0;
private final int numTasks = 12; // one for each processor
private int allFinished = 0;
private long starttime = 0;
public synchronized void updateEstimate(double est) {
long rt = System.currentTimeMillis() - starttime;
System.out.println("Terminated at " + rt + " ms, est " + est);
pi = (allFinished == 0) ? est : (pi + est) / 2;
allFinished++;
}
public double getPi() { return pi; }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
9
PI Estimation Output
(Uniproessor Hardware)
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Implementing Runnable
10
Did you notice?
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Synchronized Methods
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
11
Synchronized Blocks
• public void someMethod() {
synchronized(someObject) {
// do some stuff
}
}
• Only one thread can be running the block at any time
• The someObject is the data that is critical
• Fine grain atomicity
• Generally results in better performance
• Harder to create code
• Harder to read code later on
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Dead Locks
• If you have multiple piece of critical data:
synchronized(someObject) {
synchronized(otherObject) {
// critical section
}
}
• Always gather the locks in the same order!
• If someplace else you get otherObject before
someObject, you might end up with deadlock.
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
12
In Summary
• Multi-Threading enables use make better use of
contemporary computers:
• Prevents idle/busy waiting CPU
• Non-blocking I/O
• Parallel execution (on MP hardware)
• Automatic garbage collection
• Fun uses for games (animation / bad guys with AI)
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
13