Professional Documents
Culture Documents
Multi-Threaded
Programming
in JAVA
t0
P1
t2
P0
t1
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
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Contributions include:
Uniform interaction between hardware
Multi-user Capabilities
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Task A
Task B
Task C
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Multi-Tasking
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
Differences include:
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
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Call start()
Call start()
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
TwoThreadsTest Output
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
The Listener
public class Listener {
public static void main(String[] args) {
ServerSocket srvSock = new ServerSocket(4567);
while (keepRunning) {
//
//
//
//
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
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
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
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
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
10
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
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Dead Locks
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:
Non-blocking I/O
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
13