1. Module 08 – Java Threading
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
2. Fundamental Java Programming
The Course Outline
Module 01 – Introduction to Java
Module 02 – Basic Java Programming
Module 03 – Control Flow and Exception Handling
Module 04 – Object Oriented in Java
Module 05 – Java Package and Access Control
Module 06 – Java File IO
Module 07 – Java Networking
Module 08 – Java Threading
3. Module 08 – Java Threading
• Processes
• Threads
• Java main method as a starting thread
• Creating Thread with extending Thread class
• Creating Thread with implementing Runnable interface
• Java Thread Priority
• Network Server Thread Programming
4. Processes
A process has a self-contained execution environment.
A process generally has a complete, private set of
basic run-time resources; in particular, each process
has its own memory space.
Processes are often seen as synonymous with
programs or applications. However, what the user sees
as a single application may in fact be a set of
cooperating processes. To facilitate communication
between processes, most operating systems support
Inter Process Communication (IPC) resources, such as
pipes and sockets.
5. Threads
Multithreaded execution is an essential feature of
the Java platform. Every application has at least
one thread. From the application programmer's
point of view, you start with just one thread,
called the main thread. This thread has the ability
to create additional threads.
Eg. Two Threads are running in
a single core processor
6. Java main method as a starting thread
class TypicalClass {
public void aMethod() {
System.out.println(Thread.currentThread());
}
public static void main(String[] args) {
// main() is run in a single thread
System.out.println(Thread.currentThread());
TypicalClass hWorld = new TypicalClass();
hWorld.aMethod();
}
} Thread[main,5,main]
Thread[main,5,main]
7. Defining and Starting a Thread
1.) Runnable Interface. The Runnable interface defines a single
method, run, meant to contain the code executed in the thread.
The Runnable object is passed to the Thread constructor,
public class HelloRunnable implements Runnable {
public void run() {
System.out.println(Thread.currentThread());
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
System.out.println(Thread.currentThread());
(new Thread(new HelloRunnable())).start();
}
}
Thread[main,5,main]
Thread[Thread-0,5,main]
Hello from a thread!
8. Defining and Starting a Thread
2.) Subclass Thread. The Thread class itself implements
Runnable, though its run method does nothing. An application
can subclass Thread, providing its own implementation of run,
public class HelloThread extends Thread {
public void run() {
System.out.println(Thread.currentThread());
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
System.out.println(Thread.currentThread());
(new HelloThread()).start();
}
}
Thread[main,5,main]
Thread[Thread-0,5,main]
Hello from a thread!
9. Thread Priority
package BasicThread;
public class ThreadPriorityDemo extends Thread{
ThreadPriorityDemo(String name) {
super(name);
}
public void run() {
for (int i = 0; i<100; i++) System.out.print(getName());
}
public static void main(String args[]) {
ThreadPriorityDemo t01 = new ThreadPriorityDemo("T01");
ThreadPriorityDemo t02 = new ThreadPriorityDemo("T02");
t01.start();
t02.start();
t02.setPriority(10); // 1 min, 5 default, 10 max
}
}
10. Network Server Thread Programming
import java.io.*;
import java.net.*;
public class MyServerThread extends Thread {
ServerSocket s;
public MyServerThread() throws Exception {
super();
// Register service on port 1234
s = new ServerSocket(1234);
}
public void run() {
try {
while (true) {
Socket s1 = s.accept(); // Wait and accept a connection
// Read input from client
ObjectInputStream objInputStream = new
ObjectInputStream(s1.getInputStream());
Customer cust1 = (Customer)objInputStream.readObject();
cust1.setCollectedPoints(3000); // Dummy result
// Write result to client
OutputStream s1out = s1.getOutputStream();
ObjectOutputStream objOutputStream = new
ObjectOutputStream(s1out);
objOutputStream.writeObject(cust1);
// Close the connection, but not the server socket
objOutputStream.close();
s1out.close();
objInputStream.close();
s1.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
MyServerThread myServerThread = new MyServerThread();
myServerThread.start();
}
}
11. Client Test
import java.net.*;
import java.io.*;
public class MyClient2 {
public static void main(String[] args) throws Exception {
// Open your connection to a server, at port 1234
Socket s1 = new Socket("127.0.0.1", 1234);
// Write object to the socket server
Customer cust1 = new Customer();
cust1.setName("Somchai");
ObjectOutputStream objOutputStream =
new ObjectOutputStream(s1.getOutputStream());
objOutputStream.writeObject(cust1);
// Get result from server
InputStream s1In = s1.getInputStream();
ObjectInputStream objInputStream = new
ObjectInputStream(s1In);
Customer custResult =
(Customer)objInputStream.readObject();
System.out.println(custResult.getName() + " has " +
custResult.getCollectedPoints() + " points");
// When done, just close the connection and exit
objInputStream.close();
s1In.close();
objOutputStream.close();
s1.close();
}
}
12. Supported Class – Customer Data Object
import java.io.Serializable;
public class Customer implements Serializable{
private String name;
private int collectedPoints;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCollectedPoints(int
collectedPoints) {
this.collectedPoints = collectedPoints;
}
public int getCollectedPoints() {
return collectedPoints;
}
}
13. Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Thank you