Green Thread stop(), suspend() and resume()

Java multi threading concept is implemented by using following two models

  1. Green Thread model
  2. Native OS model

Green Thread model

  1. The Thread which is managed completely by JVM without taking underlying OS support is called Green Thread.
  2. Very few operating system like solaris os provides support for Green Thread model.
  3. Green Thread model is deprecated and not recommended to use.

Native OS model

  1. The thread which is managed by the JVM with the help of underlying OS, is called native OS model.
  2. All windows based operating system provide support for native OS model.

How to stop a thread in java ?

  1. We can stop a thread execution by using stop() method of Thread class.
  2. public void stop()
  3. If we call stop() method then immediately the thread will be entered into dead state
  4. 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 ?

  1. We can suspend a thread by using suspend() method of thread class then immediately the thread will be entered into suspended state.
  2. 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();
  3. 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);
			}
		}
	}
}

Leave a Comment