Following are the some important methods available in thread class.
Priority range of MIN_PRIORITY is 1 and MAX_PRIORITY is 10. Default value of NORMAL_PRIORITY is 5.
Output:
Daemon Thread
User Thread
User Thread
| Methods | Description |
|---|---|
| public void start( ) | This method invokes the run() method, which causes the thread to begin its execution. |
| public void run( ) | Contains the actual functionality of a thread and is usually invoked by start() method. |
| public static void sleep (long millisec) | Blocks currently running thread for at least certain millisecond. |
| public static void yield ( ) | Causes the current running thread to halt temporarily and allow other threads to execute. |
| public static Thread currentThread ( ) | It returns the reference to the current running thread object. |
| public final void setPriority (int priority) | Changes the priority of the current thread. The minimum priority number is 1 and the maximum priority number is 10. |
| public final void join (long millisec) | Allows one thread to wait for completion of another thread. |
| public final boolean isAlive ( ) | Used to know whether a thread is live or not. It returns true if the thread is alive. A thread is said to be alive if it has been started but has not yet died. |
| public void interrupt ( ) | Interrupts the thread, causing it to continue execution if it was blocked for some reason. |
Thread Priorities
Thread priorities is the integer number from 1 to 10 which helps to determine the order in which threads are to be scheduled. It decides when to switch from one running thread to another thread.Priority range of MIN_PRIORITY is 1 and MAX_PRIORITY is 10. Default value of NORMAL_PRIORITY is 5.
Daemon Thread
- Daemon thread is the low priority thread which runs in the background to perform garbage collection.
- It always depends on user threads.
- JVM does not have control over daemon thread. It can keep running even when the program has stopped.
Example : Program to create a daemon thread in Java
public class DaemonThreadDemo extends Thread
{
public void run()
{
if(Thread.currentThread().isDaemon())
{
System.out.println("Daemon Thread");
}
else
{
System.out.println("User Thread");
}
}
public static void main(String[] args)
{
DaemonThreadDemo t1 = new DaemonThreadDemo();
DaemonThreadDemo t2 = new DaemonThreadDemo();
DaemonThreadDemo t3 = new DaemonThreadDemo();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}
Output:
Daemon Thread
User Thread
User Thread


