|
Network Programming This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Lecture /
Java Programming - MultithreadingA Network Programming Lecture by Steven Choy Lecture Overview: Introduction to Multithreading - Creating and Running Threads in Java - Thread Subclass - Runnable Implementation - Other Major Thread Concepts Introduction
Examples
Single-threaded program flow
5 seconds reading file A
2 seconds processing file A
5 seconds reading file B
2 seconds processing file B
-----------------------
14 seconds total
Multi-threaded program flow
5 seconds reading file A
5 seconds reading file B + 2 seconds processing file A
2 seconds processing file B
-----------------------
12 seconds total
Benefits: Better resource utilization, Simpler program design
"If you were to program the above ordering of reading and processing by hand in a singlethreaded application, you would have to keep track of both the read and processing state of each file. Instead you can start two threads that each just reads and processes a single file. Each of these threads will be blocked while waiting for the disk to read its file. While waiting, other threads can use the CPU to process the parts of the file they have already read. The result is, that the disk is kept busy at all times, reading from various files into memory. This results in a better utilization of both the disk and the CPU. It is also easier to program, since each thread only has to keep track of a single file."
Benefit: More responsive programs
"Imagine a server application that listens on some port for incoming requests. when a request is received, it handles the request and then goes back to listening. The server loop is sketched below:"
while(server is active){
listen for request
process request
}
"If the request takes a long time to process, no new clients can send requests to the server for that duration. Only while the server is listening can requests be received."
"An alternate design would be for the listening thread to pass the request to a worker thread, and return to listening immediatedly. The worker thread will process the request and send a reply to the client. This design is sketched below:"
while(server is active){
listen for request
hand request to worker thread
}
"This way the server thread will be back at listening sooner. Thus more clients can send requests to the server. The server has become more responsive."
Creating and running threads
Thread Subclass
public class MyThread extends Thread {
public void run(){
System.out.println("MyThread running");
}
}
MyThread myThread = new MyThread();
myTread.start(); Runnable implemention
public class MyRunnable implements Runnable {
public void run(){
System.out.println("MyRunnable running");
}
}
Thread thread = new Thread(new MyRunnable());
thread.start(); Examples
Some other thread concepts you need to know for advanced multithreading programming
Multiple threads running within a program may share the same resources. Therefore, there is a possibility that more than one thread can access the same object at the same time. Without taking appropriate care this could lead to what is known as a synchronization problem.
The life of a thread ends when its run method finishes execution. Sometimes, you may want to terminate a thread from another thread.
Class Exercises
The following code segment may be useful to you.
public void run() {
try {
for(int i = 100; i > 0; i--) {
System.out.println(horsename + "--> " + i);
Thread.sleep((long) (Math.random() * 100));
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Horse " + horsename + " finishes");
}
ReadingThanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. If you find any problem in this lecture note, please feel free to tell Steven via steven@findaway.hk. |