public class MyThread1 extends Thread - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

public class MyThread1 extends Thread

Description:

x.start(); // DO NOT CALL run()!!! y.start(); // DO NOT CALL run ... public class ClassToRun extends SomeClass implements Runnable { public void run ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 9
Provided by: itc125
Category:

less

Transcript and Presenter's Notes

Title: public class MyThread1 extends Thread


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 lt 100 i)
System.out.print(Hello \n) public class
MyThread2 extends Thread public void start()
public void run() for(int i 0 i
lt 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()
run()
Step1 Execute 4 lines Then stop the execution
MyThread2
MyThread1
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
CPU
4
THREADED GUI-BASED APPLICATION 1
Main Frame
Panel 2
Panel 1
JButton
Start Ball Bouncing
Each ball is re-drawn after 1 sec at a new random
location within its own panel!
5
THREADED GUI-BASED APPLICATION 2
Main Frame
0
Textfield
Start
JButton
User clicks on Start then the textfield
increments each second by 1!
6
THREADED GUI-BASED APPLICATION 2 (Contd)
Main Frame
1003
Pause
User clicks on Pause then the textfield stops
incrementing
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!!!
Write a Comment
User Comments (0)
About PowerShow.com