Thread is a lightweight process. Threads exist within a process. So every process has at least one thread. Thread in Java is represented by java.lang.Thread
class. Every Java application has one or several threads.
- Threads are lightweight. Creating a new thread requires fewer resources than creating a new process.
- Threads share process’s resources like memory space.
- Context switching between threads is usually less expensive than switching between processes.
- Cost of intercommunication between threads is relatively low than that of communication between processes.
There are two ways to create thread in Java:
- By implementing the
Runnable
interface. - By extending
java.lang.Thread
class.