Deprecated Methods of Thread Class in Java : Free Java Tutorials

There are three deprecated methods in the Thread Class as of now. A deprecated method is the one that is discouraged from practice by programmers, typically because it is dangerous, or because a better alternative exists. We will see in detail about such methods in this tutorial.

Java multithreading concept is implemented by using the following two models.

  1. Green Thread Model
  2. Native OS Model

Green Thread Model

The thread which is managed completely by JVM without any kind of underlying OS support is called Green Thread.

Very few Operating Systems provide support for the Green Thread model. For example, Sun Solaris OS provides such support.

Anyways, this 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 support is called Native OS model.

All windows based operating systems provide support for native OS model.

Example of Deprecated Methods of Thread Class

There are 3 deprecated methods in the Thread Class and they are as following:

How to stop a Thread

We can stop a thread execution by using public void stop() method of Thread class.

If we call stop() method, then thread will immediately enter into dead state. But, this stop() method is deprecated and not recommended to use.

class ChildThread extends Thread
{
	public void run()
	{
		for(int i=0;i<10;i++)
		{
			try
			{
				Thread.sleep(2000);
			}
			catch(InterruptedException exp)
			{
				System.out.println("Child Thread got interrupted");
			}
			System.out.println("Child Thread is executing");
		}
	}
}

public class Demo 
{
	public static void main(String[] args) throws InterruptedException
	{
		System.out.println("Main Thread has started its execution");
		ChildThread child_thread = new ChildThread();
		child_thread.start();
		Thread.sleep(11000);
		child_thread.stop();
		System.out.println("Main Thread has finished its execution");		
	}
}

In the above code, you can see that when stop() method is called, Eclipse editor puts a line on the word() stop and gives a compile-time warning which says: The method stop() from the type Thread is deprecated since version 1.2

However, the program will still run successfully. Both threads will start concurrently.

The main thread will enter into the sleep state for 11 seconds. During which, the child thread will print a statement 5 time.

After that, the main thread will call stop() method on child thread. So, child thread will immediately get killed and will enter into dead state without printing the statement five more time.

Look at the output below for better understanding.

Output:

Main Thread has started its execution
Child Thread is executing
Child Thread is executing
Child Thread is executing
Child Thread is executing
Child Thread is executing
Main Thread has finished its execution

How to suspend and resume a Thread

We can suspend a thread by using public void suspend() method of Thread class.

By calling suspend() method, the thread will immediately enter into the suspended state.

We can resume suspended thread by calling public void resume() method of Thread class so that suspended thread will resume and continue its execution.

However, these two methods are also deprecated and they are not recommended to use.


Impact of these deprecated methods on the life cycle of a thread

 

Deprecated-Methods-of-Thread-Class

In the above diagram, new states and transitions are added which are required when stop(), suspend() or resume() method is called.

This is all about the deprecated methods of the Thread class. hope you find this tutorial helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *