1 / 18

Java Threads

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.

lacey
Download Presentation

Java Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Threads A tool for concurrency

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

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

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

  5. But the main thread can generate other threads main() a() b() b() c() 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 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

  8. 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();

  9. 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(); } }

  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(); } } 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(); } }

  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(); } } 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(); } }

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

More Related