Open In App

Green vs Native Threads and Deprecated Methods in Java

Last Updated : 16 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A multi-threaded process as we all know has several sequences of control, where each sequence is governed by a thread and is capable of performing independent tasks concurrently. In Java, multithreading concept is implemented by using the following two models.

  1. Green Thread model 
  2. Native Thread Model 

Green Thread Model 

  • In this model, threads are completely managed by JVM without any kind of underlying OS support.
  • These threads are implemented at the application level and managed in user space.
  • They are also called cooperative (user-level) threads.
  • Only one green thread can be processed at a time. Hence, this model is considered the many-to-one model.Because of this, green threads can run on multi-core processors but cannot take advantage of multiple cores.
  • Synchronization and resource sharing is easier for green threads and hence execution time is also less.
  • Sun Solaris OS provides support for Green Thread model.

Native Thread Model 

  • Threads in this model are managed by the JVM with the help of underlying OS support.
  • These threads are implemented at the OS level (by using OS multithreading API) and managed in kernel space.
  • They are also called non-green (kernel-level) threads.
  • Multiple native threads can coexist. Therefore it is also called many-to-many model. Such characteristic of this model allows it to take complete advantage of multi-core processors and execute threads on separate individual cores concurrently.
  • Since this model allows multiple threads to be executed simultaneously, thread synchronization and resource sharing become complicated. This increases execution time of threads.
  • All the windows based OS provide support for Native Thread model.

Because of its limitations, the Green Thread model is deprecated and no longer used. Native Thread model has replaced Green Thread model and it is used widely today. Apart from this, the Thread class in Java has some methods which are deprecated.

  1. public void stop()
    This method is deprecated because it is inherently unsafe to use. For example, suppose a thread has opened a file and is writing into the file. Suddenly, if we call stop() method on this thread, then the thread will terminate abruptly and the file will remain open. 
    This is potentially considered unsafe. Hence, we use inter-thread communication and interrupt system to signal a thread to stop its execution for some time instead of forcing the thread to stop its execution.
  2. public void suspend() 
    Thread.suspend() method is deprecated because it is deadlock prone. Consider an example where a primary thread holds a lock on an object and that thread is suspended. No other thread can get that object level lock until that thread is resumed. If a secondary thread tries to acquire the lock of that object before the primary thread resumes, then there is a deadlock situation.
  3. public void resume() 
    Since suspend() method is deprecated, resume() method is also deprecated.

Following example illustrates how use of suspend() method may lead to Deadlock. 

JAVA




// Printer class
class Printer {
 
    // synchronized print() method
    public synchronized void print()
    {
        System.out.println(Thread.currentThread().getName() +
                                        " wants to print");
 
        // making sure that primary thread is getting suspended
        if (Thread.currentThread().getName().equals("Primary Thread")) {
            System.out.println("Primary Thread has been suspended");
            Thread.currentThread().suspend();
            System.out.println("Primary Thread has been resumed");
        }
 
        System.out.println(Thread.currentThread().getName()
                        + " has finished printing");
    }
}
 
// Custom Thread class that extends Thread class
class CustomThread extends Thread {
    // string variable that will store name of the thread
    Printer obj;
 
    // constructor to initialize thread object
    CustomThread(String name, Printer p)
    {
        // calling Thread class constructor and naming
        // the thread
        super(name);
        obj = p;
    }
 
    // overridden run() method
    public void run()
    {
        // calling synchronized print() method
        obj.print();
 
        // printing a statement that confirms the
        // end of execution of the thread
        System.out.println(this.getName() +
                    " has finished its execution");
    }
}
 
// Driver class
class Deadlock {
    // Main method which thros InterruptedException
    // that can be caused as we are calling sleep() method
    public static void main(String[] args) throws InterruptedException
    {
        // Creating Printer object in order to print
        Printer p = new Printer();
 
        // Creating primary thread and starting its execution
        CustomThread primary_thread = new CustomThread("Primary Thread", p);
        System.out.println("Primary Thread is starting its execution");
        primary_thread.start();
 
        // Making main thread to sleep for 1 second to make sure that
        // primary thread executes print() method and goes into suspend state
        Thread.sleep(2000);
 
        // Creating secondary thread and starting its execution
        CustomThread secondary_thread = new CustomThread("Secondary Thread", p);
        System.out.println("Secondary Thread is starting its execution");
        secondary_thread.start();
    }
}


Output: 

Primary Thread is starting its execution
Primary Thread wants to print
Primary Thread has been suspended
Secondary Thread is starting its execution

Explanation: In the above program, we create two threads called primary thread and secondary thread and start both threads in the respective order after a time interval of 2 seconds. 
Primary thread gets executed first and it gets suspended while executing the print() method. It possesses the lock of the object of Printer class while remaining in the suspended state. After that, the secondary thread tries to execute print() method. But it cannot execute it because the lock of the Printer object is with the primary method. 
At this stage, both threads enter into deadlock state and the program execution will never end. We can see that only 4 statements are printed in the output and rest other statements are not printed because the program execution enters into deadlock state. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads