Thread Examples Runnable Interface Runnable defines only one
Thread Examples
Runnable Interface • Runnable defines only one abstract method; Public void run(); • Thread also implements Runnable interface. • Why does java provide this weird interface? • Suppose we want to make an applet named Want. To. Be. Thread and we want to have a thread inside this applet. In other words, we want Want. To. Be. Thread to share its method with another thread. • This is not an easy situation since a class can not extend two or more classes. • The solution is to implement Runnable interface with some trick.
Runnable Interface cont. • Remember that we need a thread anyway. Let’s call the thread as Run. For. You. • The idea is to make run() method of Want. To. Be. Thread applet as the starting point of Run. For. You. • This can be viewed as replacing the run() method of Run. For. You with that of Want. To. Be. Thread applet. • As a result, when Want. To. Be. Thread calls start() of Run. For. You, run() of Want. To. Be. Thread will be called eventually.
class Want. To. Be. Thread extends Applet implments Runnable { Thread Run. For. You; public void start() { if ( Run. For. You == null ) { Run. For. You = new Thread(this); // Note this is Runnable Run. For. You. start(); } public void run() { // Run. For. You runs here!!! } public void stop() { Run. For. You = null; // Will be explained later. } }
Server Example • In client/server model, the server part is generally designed to wait indefinitely for a request from a client. • Once a request comes in, the server executes the corresponding service routine for the request. • Let’s suppose we implement a server (can be any type of server), which is single threaded. • This means when the server is in the middle of processing a client request, no further request gets serviced until the server finishes with the current request. • This may result in servicing bottleneck especially the servicing routine contains any blocking call.
class Server extends JFrame implements Action. Listener { Server() { Container pane = get. Content. Pane(); JButton button = new JButton("Request"); button. add. Action. Listener(this); pane. add(button); set. Size(300, 200); set. Visible(true); } public void action. Performed(Action. Event evt) { do_service(); } public static void main(String args[]) { new Server(); private void do_service() { } for (int I=0; I<10; I++) { try { Thread. sleep(1000); } catch (Interrupted. Exception ie) {} System. out. println(“count down “ + (10 – I)); } }
class Server 2 implements Action. Listener { int req=0; ……… public void action. Performed(Action. Event evt) { new Service. Handler(req++). start(); } } class Service. Handler extends Thread { int req; Service. Handler(int req) { this. req = req; } public void run() { for (int i=0; i<10; i++) { try { sleep(1000); }catch (Interrupted. Exception ie) { } System. out. println("handling req " + req + ": " + (10 -i)); } System. out. println(" handling req " + req + " done"); } }
class Server 3 extends JFrame implements Action. Listener, Runnable { Server 3() { // The same constructor as Server} public void action. Performed(Action. Event evt) { new Thread(this). start(); } public void run() { for (int i=0; i<10; i++) { try { Thread. sleep(1000); } catch (Interrupted. Exception ie) {} System. out. println("handling request: " + (10 -i)); } System. out. println(" handling request done"); } public static void main(String args[]) { new Server 3(); } }
Test Prime Number • Often we want to solve a problem by dividing it into a set of smaller problems. • Let’s suppose we want to build a program that tests whether the given number is a prime number. • Even though there are good algorithms that do the job efficiently, we are going to use the very basic one here. (Divide by every number within the range) • To demonstrate the power of parallel programming, we split the range into several sub ranges and let different threads test different sub ranges.
public class Prime. Checker { public static void main(String args[]) { long number = Long. parse. Long(args[0]); interval = (int)(number/100) + 1; for (int i=0; i<interval; i++) { new Check. Range(i*100, number). start(); } } }
class Check. Range extends Thread { long base, number; Check. Range(long base, long number) { if ( base == 0 ) this. base = 2; else this. base = base; this. number=number; } public void run() { for (long i=base; i<base+100; i++) { if ( number%i == 0 ) { System. out. println(number + " is divisible by " + i + " found by " + get. Name()); break; } yield(); } } }
- Slides: 11