200 likes | 367 Views
Java Threads. A tool for concurrency. OS schedules processes. Ready. Running. Blocked. 205 198 201. 200. 177 206 180 185. A process loses the CPU and another one takes over. If it blocks (200), the process must wait for i/o to complete. What’s different in a thread?. Ready.
E N D
Java Threads A tool for concurrency
OS schedules processes Ready Running Blocked 205 198 201 200 177 206 180 185 A process loses the CPU and another one takes over. If it blocks (200), the process must wait for i/o to complete.
What’s different in a thread? Ready Running Blocked If the process is threaded blocking one thread does NOT force giving up the CPU 205 198 201 200 177 206 180 185 OS manages threads in a process like it manages processes. Thread D may block but another thread of the same process (A) will start running. Ready Running Blocked 200A 200B 200C 200D 200E 200F 200G 200H
All programs have at least one thread main() a() b()
But the main thread can generate other threads main() a() b() b() c() c()
How to create a thread? • c/c++ provides for any function to be potentially be a thread. • A special class must be created in order to define the thread. • Consider the previous example. • b() represents the thread
c++ thread void main() { _beginthread(( (void(*) void()) count, 0 , (void*) 4); count(4); } void count(int i) {int j; for (j=1; j<=i; j++) cout << j<<endl; } 1 2 1 2 3 3 4 4 1 2 1 2 3 3 4 4
public class b extends Thread { public b(String str) { super(str); } void c() { … } public void run() { // code for thread b c(); } } thread definition creates and launches the thread. new b(“firstb").start();
Entire application public class b extends Thread { public b(String str) { super(str); } void c() { … } public void run() { // code for thread b c(); } } main() a() b() b() c() c() public class testthreads{ void a(){ … } public static void main (String[] args) { a(); new b(“firstb").start(); new b(“secondb").start(); } }
public class b extends Thread { String val; public b(String str) { super(str); val=str; } void c() {System.out.println(“In C”); } public void run() { // code for thread b System.out.println(this+” “+val); System.out.println(“In B”); c(); } } Output: In A bR@1a16869 firstb In B In C bR@1cde100 secondb In B In C public class testthreads{ static void a(){System.out.println(“In A”); } public static void main (String[] args) { a(); new b(“firstb").start(); new b(“secondb").start(); } }
A special problem • Java does not allow for multiple inheritance. • How do you create a thread (extends Thread) when your class is a subclass? • Applet classes must inherit Applet. • So how do you make Applet Thread? • Interfaces provide solution to multiple interface problem • Interface needed is Runnable.
public class bR implements Runnable { String val; public bR(String str) { val=str; } void c() {System.out.println(“In C”); } private Thread bRT; public void start() { bRT = new Thread(this,val); bRT.start(); } public void run() { // code for thread b System.out.println(this+” “+val); System.out.println(“In B”); c(); } } Output: In A bR@ 1a firstb Thread[secondb,5,main] secondb In B In B In C In C public class testthreads{ static void a(){System.out.println(“In A”); } public static void main (String[] args) { a(); new bR(“firstb").start(); new bR(“secondb").start(); } }
Using Threaded Servers • First examine sockets in java • Second examine using sockets in a server • Third examine using threaded servers
Java sockets • Interface is much simpler that c/c++ • Creation is intuitive. • Create a standard java stream (input, output, or both … both more typical) • Hook the java stream to the socket stream • Remember they are references • Use the stream to read and write socket.
Create a socket int serviceport=12345; try { serverSocket = new ServerSocket(serviceport); } catch (IOException e) { System.out.println("Could not listen on port: ”+ serviceport); System.exit(-1); } • Creates a TCP (not UDP) socket • Uses port number as a parameter • Obviously the IP is itself • Must provide exception handlers as above
Accept connection Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept ERROR"); … } • Identical to berkley socket accept() • Returns socket for client interaction • Must provide exception handler as above
Connect streams to socket PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader ( clientSocket.getInputStream())); • This is NEW! • Create • PrintWriter • BufferedReader • Hook to clientsocket • Output stream • Input stream
Socket I/O // an echo server // read and write back inputLine = in.readLine(); while (inputLine != null) { outputLine = PROCESS(inputLine); out.println(outputLine); if outputLine.equals(“...")) break; }