Title: public class MyThread1 extends Thread
1public 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()!!!
2public 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()!!!
3run()
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
4THREADED 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!
5THREADED GUI-BASED APPLICATION 2
Main Frame
0
Textfield
Start
JButton
User clicks on Start then the textfield
increments each second by 1!
6THREADED 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()
-
- . . .
8public void meth1() try //
Thread.sleep(1000) // Stop execution for 1
sec!!!! catch(InterruptedException iec)
System.exit(-1) // SOMETHING IS WRONG!!!