- Non Static methods of Thread class
- start()
- stop()
- suspend() — depricated
- resume() — — depricated
Java multi threading concept is implemented by using following two models
- Green Thread model
- Native OS model
Green Thread model
- The Thread which is managed completely by JVM without taking underlying OS support is called Green Thread.
- Very few operating system like solaris os provides support for Green Thread model.
- Green Thread model is deprecated and not recommended to use.
Native OS model
- The thread which is managed by the JVM with the help of underlying OS, is called native OS model.
- All windows based operating system provide support for native OS model.
How to stop a thread in java ?
- We can stop a thread execution by using stop() method of Thread class.
- public void stop()
- If we call stop() method then immediately the thread will be entered into dead state
- stop() method is deprecated and not recommended to use.
public class DemoThread {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
System.out.println("End of main Thread");
myThread.stop();
}
}
class MyThread extends Thread{
public void run() {
for(int i = 0; i<10; i++) {
System.out.println("Executing child thread "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(e);
}
}
}
}
How to suspend and resume a thread in java ?
- We can suspend a thread by using suspend() method of thread class then immediately the thread will be entered into suspended state.
- We can resume a suspended thread by using resume() method of thread class, then suspended thread can continue its execution.
public void suspend();
public void resume(); - These methods are deprecated and not recommended to use.
public class DemoThread {
public static void main(String[] args) throws InterruptedException {
MyThread myThread1 = new MyThread();
myThread1.start();
System.out.println("End of main Thread");
Thread.sleep(2000);
System.out.println("Suspending child thread");
myThread1.suspend();
System.out.println("Main thread will sleep for 10 seconds");
Thread.sleep(10000);
System.out.println("Resuming child thread");
myThread1.resume();
}
}
class MyThread extends Thread{
public void run() {
for(int i = 0; i<10; i++) {
System.out.println("Executing child thread "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(e);
}
}
}
}
