Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Threads A tool for concurrency. OS schedules processes Ready 205 198 201 Running 200 Blocked 177 206 180 185 A process loses the CPU and another.

Similar presentations


Presentation on theme: "Java Threads A tool for concurrency. OS schedules processes Ready 205 198 201 Running 200 Blocked 177 206 180 185 A process loses the CPU and another."— Presentation transcript:

1 Java Threads A tool for concurrency

2 OS schedules processes Ready 205 198 201 Running 200 Blocked 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.

3 What’s different in a thread? If the process is threaded blocking one thread does NOT force giving up the CPU Ready 205 198 201 Running 200 Blocked 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 200A 200B 200C Running 200D Blocked 200E 200F 200G 200H

4 All programs have at least one thread main() a() b()

5 But the main thread can generate other threads main() a() b() c()

6 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

7 c++ thread void count(int i) {int j; for (j=1; j<=i; j++) cout << j<<endl; } 1212334412123344 void main() { _beginthread(( (void(*) void()) count, 0, (void*) 4); count(4); } 1212334412123344

8 public class b extends Thread { public b(String str) { super(str); } void c() { … } public void run() { // code for thread b c(); } new b(“firstb").start(); thread definition creates and launches the thread.

9 Entire application public class b extends Thread { public b(String str) { super(str); } void c() { … } public void run() { // code for thread b c(); } public class testthreads{ void a(){ … } public static void main (String[] args) { a(); new b(“firstb").start(); new b(“secondb").start(); } } main() a() b() c()

10 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(); } 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(); } } In A bR@1a16869 firstb In B In C bR@1cde100 secondb In B In C Output:

11 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.

12 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(); } 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(); } } In A bR@ 1a firstb Thread[secondb,5,main] secondb In B In C Output:

13 Using Threaded Servers First examine sockets in java Second examine using sockets in a server Third examine using threaded servers

14 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.

15 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

16 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

17 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

18 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; }


Download ppt "Java Threads A tool for concurrency. OS schedules processes Ready 205 198 201 Running 200 Blocked 177 206 180 185 A process loses the CPU and another."

Similar presentations


Ads by Google