Creating Java native OS threads
The term native thread refers to threads managed by the computer’s OS. When we create a Java native thread, we are referring to threads that the JVM manages using the underlying OS’s threads library API. This also means that the JVM deals with the different threads libraries on different OSs, while we use the Java API to create threads. A program that employs threads written on an Apple Mac will work on a Windows machine as the JVM handles the lowest levels of threads.
We will look at three different ways to create Java native threads and one way to create a pool of threads. These will involve the following:
- Extending the
Thread
class - Implementing the
Runnable
interface - Creating a thread pool with
ExecutorService
- Implementing the
Callable
interface - Managing threads
The final items we will cover are the following:
- Daemon and non-daemon threads
- Thread priority