Program for showing Thread class methods.

The below program shows some of the method of Thread class. The minimum, normal and maximum priority of thread are 1, 5 and 10 respectively. Method with its explanation are given below:
  • static Thread currentThread( ) : Returns a reference to the currently executing thread object.
  • long getId( ) : Returns the identifier of Thread.
  • String getName( ) : Returns thread's name.
  • int getPriority( ) : Returns thread's priority.
  • Thread.State getState( ) : Returns the state of thread.
  • ThreadGroup getThreadGroup( ) : Returns the thread group to which thread belongs.
  • boolean isAlive( ) : Tests if the thread is alive.
  • boolean isDaemon( ) : Tests if the thread is a daemon thread.
  • void join(long millis) : Waits at most millis milliseconds for the thread to die.
  • void setDaemon(boolean on) : Marks thread as either a daemon thread or a user thread.
  • void setName(String name) : Changes the name of thread specified by name.
  • void setPriority(int newPriority) : Changes the priority of thread.
  • static void sleep(long millis) : Causes the currently executing thread to sleep for the specified number of milliseconds
  • static Thread currentThread() : Returns a reference to the currently executing thread object.


PROGRAM
public class ThreadMethodsDemo {

 public static void main(String[] args) {
  
  System.out.println("Thread Minimum Prority : "+Thread.MIN_PRIORITY);
  System.out.println("Thread Normal Prority : "+Thread.NORM_PRIORITY);
  System.out.println("Thread Maximum Prority : "+Thread.MAX_PRIORITY);
  Thread t = Thread.currentThread();
  System.out.println("Current Thread details : "+t.currentThread());
  t.setName("Main Thread");
  System.out.println("After changing name of main thread : "+t.currentThread());
  System.out.println("Thread ID : "+t.getId());
  System.out.println("Thread Name : "+t.getName());
  System.out.println("Thread Priority : "+t.getPriority());
  System.out.println("Thread State : "+t.getState());
  System.out.println("Thread Group : "+t.getThreadGroup());
  System.out.println("Is Daemon Thread  : "+t.isDaemon());
  t.setPriority(7); 
  // If priority greater than 10 or less than 1 then IllugalArgumentException is thrown.
  System.out.println("After changing priority it becomes : "+t.getPriority());
 }
}
OUTPUT
C:\>javac ThreadMethodsDemo.java
C:\>java ThreadMethodsDemo
Thread Minimum Prority : 1
Thread Normal Prority : 5
Thread Maximum Prority : 10
Current Thread details : Thread[main,5,main]
After changing name of main thread : Thread[Main Thread,5,main]
Thread ID : 1
Thread Name : Main Thread
Thread Priority : 5
Thread State : RUNNABLE
Thread Group : java.lang.ThreadGroup[name=main,maxpri=10]
Is Daemon Thread  : false
After changing priority it becomes : 7

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet