Presentation is loading. Please wait.

Presentation is loading. Please wait.

Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread.

Similar presentations


Presentation on theme: "Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread."— Presentation transcript:

1 public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread { public void start() { } public void run() { System.out.print(“Bye \n”) } public class TestThreads { public static void main(String[] args) { MyThread1 x = new MyThread1(); MyThread2 y = new MyThread2(); x.start(); // DO NOT CALL run()!!! y.start(); // DO NOT CALL run()!!! }

2 public class MyThread1 extends Thread { public void start() { } public void run() { for(int i = 0; i < 100; i++) System.out.print(“Hello \n”) } public class MyThread2 extends Thread { public void start() { } public void run() { for(int i = 0; i < 100; i++) System.out.print(“Bye \n”) } public class TestThreads { public static void main(String[] args) { MyThread1 x = new MyThread1(); MyThread2 y = new MyThread2(); x.start(); // DO NOT CALL run()!!! y.start(); // DO NOT CALL run()!!! }

3 run() MyThread1 MyThread2 CPU Step1: Execute 4 lines Then stop the execution Step2: Execute 3 lines then Stop the execution CPU collaborates with the JVM during Execution. The JVM schedules the jobs To be done by the CPU : JVM has a sub-program called: SCHEDULER

4 Panel 1 Panel 2 Main Frame Each ball is re-drawn after 1 sec at a new random location within its own panel! Start Ball Bouncing THREADED GUI-BASED APPLICATION 1 JButton

5 0 Main Frame User clicks on Start then the textfield increments each second by 1! Start THREADED GUI-BASED APPLICATION 2 Textfield JButton

6 1003 Main Frame User clicks on Pause then the textfield stops incrementing Pause THREADED GUI-BASED APPLICATION 2 (Cont’d)

7 public class ClassToRun extends SomeClass implements Runnable { public void run() { // Fill this as if ClassToRun // were derived from Thread }... public void startThread() { // Just to start the thread!!! ClassToRun ref = new ClassToRun(); Thread theThread = new Thread(ref); theThread.start(); }... }

8 public void meth1() { try { // Thread.sleep(1000); // Stop execution for 1 sec!!!! } catch(InterruptedException iec) { System.exit(-1); // SOMETHING IS WRONG!!! }


Download ppt "Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread."

Similar presentations


Ads by Google