Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does.

Similar presentations


Presentation on theme: "Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does."— Presentation transcript:

1 Thread Examples

2 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 WantToBeThread and we want to have a thread inside this applet. In other words, we want WantToBeThread 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.

3 Runnable Interface cont. Remember that we need a thread anyway. Let’s call the thread as RunForYou. The idea is to make run() method of WantToBeThread applet as the starting point of RunForYou. This can be viewed as replacing the run() method of RunForYou with that of WantToBeThread applet. As a result, when WantToBeThread calls start() of RunForYou, run() of WantToBeThread will be called eventually.

4 class WantToBeThread extends Applet implments Runnable { Thread RunForYou; public void start() { if ( RunForYou == null ) { RunForYou = new Thread(this); // Note this is Runnable RunForYou.start(); } public void run() { // RunForYou runs here!!! } public void stop() { RunForYou = null; // Will be explained later. }

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

6 class Server extends JFrame implements ActionListener { Server() { Container pane = getContentPane(); JButton button = new JButton("Request"); button.addActionListener(this); pane.add(button); setSize(300,200); setVisible(true); } public void actionPerformed(ActionEvent 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 (InterruptedException ie) {} System.out.println(“count down “ + (10 – I)); }

7 class ServiceHandler extends Thread { int req; ServiceHandler(int req) { this.req = req; } public void run() { for (int i=0; i<10; i++) { try { sleep(1000); }catch (InterruptedException ie) { } System.out.println("handling req " + req + ":" + (10-i)); } System.out.println(" handling req " + req + " done"); } class Server2 implements ActionListener { int req=0; ……… public void actionPerformed(ActionEvent evt) { new ServiceHandler(req++).start(); }

8 class Server3 extends JFrame implements ActionListener,Runnable { Server3() { // The same constructor as Server} public void actionPerformed(ActionEvent evt) { new Thread(this).start(); } public void run() { for (int i=0; i<10; i++) { try { Thread.sleep(1000); } catch (InterruptedException ie) {} System.out.println("handling request:" + (10-i)); } System.out.println(" handling request done"); } public static void main(String args[]) { new Server3(); }

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

10 public class PrimeChecker { public static void main(String args[]) { long number = Long.parseLong(args[0]); int interval = (int)(number/100) + 1; for (int i=0; i<interval; i++) { new CheckRange(i*100,number).start(); }

11 class CheckRange extends Thread { long base,number; CheckRange(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 " + getName()); break; } yield(); }


Download ppt "Thread Examples. Runnable Interface Runnable defines only one abstract method; Public void run(); Thread also implements Runnable interface. Why does."

Similar presentations


Ads by Google