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.
- 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.
- 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.
- 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.
Primary Thread is starting its execution Primary Thread wants to print Primary Thread has been suspended Secondary Thread is starting its execution
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference Between Daemon Threads and User Threads In Java
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- native keyword in java
- Static methods vs Instance methods in Java
- Killing threads in Java
- Joining Threads in Java
- Output of Java program | Set 16 (Threads)
- Introducing Threads in Socket Programming in Java
- Producer-Consumer solution using threads in Java
- Understanding threads on Producer Consumer Problem | Java
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.BufferedWriter class methods in Java
- Methods in Java
- LongBuffer get() methods in Java
- BigInteger max() and min() Methods in Java
- Java Interface methods
- IntBuffer get() methods in Java | Set 1
- CharBuffer get() methods in Java
- CharBuffer put() methods in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.