Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads CS 21b. Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs.

Similar presentations


Presentation on theme: "Threads CS 21b. Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs."— Presentation transcript:

1 Threads CS 21b

2 Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs we have written up to this point have been on a single thread of control

3 The Thread class zThread creation Õinstantiate a class that extends Thread Õinstantiate the class Thread but provide a run() method zThread execution Õlet t refer to a thread object Õt.start() causes the thread to execute its run() method “in the background”

4 Example 1: PrintThread zPrintThread class extends Thread zAttributes: name & timedelay zConstructor sets name and timedelay zrun method prints name twice but with a time delay in between the print statements Õuse Thread method sleep(timedelay)

5 run() Method for PrintThread public class PrintThread extends Thread {... public void run() { System.out.println(name); try { sleep(timedelay); } catch(Exception e) {} System.out.println(name); } … }

6 Using PrintThread PrintThread t1,t2,t3; t1 = new PrintThread("tic",5000); t2 = new PrintThread("tac",1000); t3 = new PrintThread("toe",3000); t1.start(); t2.start(); t3.start(); // expected output ? last output line should be tic

7 Example 2: MovingCircle zMovingCircle class extends Applet zAttributes: xpos & size (of circle) Õprovide initial values zpaint() method: draws the circle zThread created inside init() Õrun() method has a loop that continuously updates xpos & size and then repaints zA button executes the thread (t.start())

8 MovingCircle class (attributes & paint()) // sample animation using Threads public class MovingCircle extends Applet { int xpos = 5; // these variables will be updated int size = 10; // on a different thread... public void paint(Graphics g) { g.drawOval(xpos,50,size,size); } … }

9 MovingCircle class (thread code) t = new Thread() { public void run() { while (xpos < 80) { try {sleep(1000);} catch(Exception e) {} xpos +=5; size += 3; repaint(); // forces paint() to be re-executed } }}; // this statement placed inside the init() method

10 The Runnable Interface zRunnable is an interface that contains the run() method zOne of the constructors for Thread: public Thread(Runnable r) zCan create a thread by giving it an argument (object) that implements run() Õalternative to extending thread and then overriding run()

11 Thread Issues zDeadlock Õsome methods have been deprecated because of this possibility zSynchronization Õseveral threads running the same method and/or updating the same data

12 Networking CS 21b

13 Client/Server Computing zCommunication over the network often occurs between a client and a server zA server listens for connection requests and then responds to messages zA client establishes a connection to a server and then sends messages zTCP/IP: abstract layer that simplifies the above activities

14 Hosts, Ports, and Sockets zComputers on the network are (uniquely) specified by a host name or an IP address zCommunication between hosts occurs through their ports Õeach port allows several connections zSocket Õconnection handle that facilitates communication over the network

15 Networking in Java zjava.net package Õimport java.net.*; zMost important classes ÕSocket ÕServerSocket ÕURL (discussed earlier)

16 The Socket class zConstructor requires a host and port that the client intends to connect to zUseful Socket methods ÕInputStream getInputStream() ÕOutputStream getOutputStream() zUse file/stream interfaces to carry out the communication

17 The ServerSocket class zConstructor requires a port number that the server wishes to listen from zaccept() method returns a Socket object once a connection from a client has been established Õblocks (hangs) until the connection occurs

18 On the Server Program... zCreate a ServerSocket object Õspecify port zInvoke accept() on that object zObtain Socket object Õreturned by accept() zObtain I/O streams from the socket Õcarry out communication using these streams

19 On the Client Program... zCreate Socket object Õspecify host and port of server zObtain I/O streams from the socket Õcarry out communication using these streams * execute the server before the client

20 Allowing Multiple Connections to a Server zUse Threads zHave a loop that continuously calls accept() Õmain thread zCreate and start a thread whenever accept() returns a socket Õfacilitate communication on the socket using a separate thread


Download ppt "Threads CS 21b. Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs."

Similar presentations


Ads by Google