MULTITHREADING
MULTITHREADING
CPU
main
run
GC
begin
body
end
7
Main Thread
start
start start
Internet
Server
Local Area Network
PDA
In both cases the run() method should be implemented
10
class MyThread extends Thread {
public void run() {
System.out.println(" this thread is running ... ");
}
}
class ThreadEx1 {
public static void main(String [] args ) {
MyThread t = new MyThread();
t.start();
}
}
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t From ThreadA: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread {
public void run()
{
for(int i=6;i<=10;i++)
{
System.out.println("\t From ThreadB: i= "+i);
}
System.out.println("Exit from B");
}
}
class C extends Thread {
public void run()
{
for(int i=11;i<=15;i++) {
System.out.println("\t From ThreadC: i= "+i);
}
System.out.println("Exit from C");
}
}
Newborn
new ThreadExample(); Thread
stop()
thread.start();
wait() resume()
sleep() notify() stop()
Suspend()
Newborn
stop();
start();
Runnable Threads
Running Thread
suspend()
resume()
after(t)
notify
Runnable Waiting
Running
Demo
ThreadMethods
start()
Ready queue
Newly created
threads
Currently executed
thread
I/O operation completes