All About Class Thread in Java



Thread class allows us to run multiple programs at a time.A Thread corresponds to the control of program. Every Thread has its own priority.Threads with higher priority are executed first than the lower priority Threads. Each thread may or may not be marked as Daemon Thread.
During execution,it a Thread creates another child Thread, the child Thread will have the same priority as parent Thread.Similarly if a Daemon Thread creates a child Thread, it will be automatically Daemon Thread.When java virtual machine continue to execute , there is usually a single Thread(non Daemon) which calls the main method of some class.The JVM continue to execute threads until one of the following occurs:

  1. The exit() method of class Runtime has been called and the Security Manager has permitted the exit operation.
  2. All Threads that are not Daemon Threads have died either by run time from the call to the run() method or by performing the stop() method.
The Structure of the Thread Class is given below:

public class java.lang.Thread extends java.lang.Object implements java.lang.Runnable{
//Member Elements
public final static int MAX_PRIORITY; // Max priority of a Thread is 10
public final static int MIN_PRIORITY; // Min priority of a Thread is 0
public final static int NORM_PRIORITY; // NORM priority of a Thread is 5
//Constructors
public Thread();
//Creates and allocates a new Thread object.This constructor is same as Thread(null,null,gName), where gName
//is newly generated name. This is automatic assignment in the form of "Thread-"+n where n is an integer.
public Thread(Runnable target);
//Creates and allocates a new Thread object.This constructor is same as Thread(null,null,gName), where gName 
//is newly generated name. This is automatic assignment in the form of "Thread-"+n where n is an integer.
//target is the object whose run() method is called.
public Thread(Runnable target,String name);
//Creates and allocates a new Thread object.This constructor has the same effect as Thread(null,target,name). 
//target is the object whose run() method is called. and name is the name of the Thread.
public Thread(String name);
//Creates and allocates a new Thread object whose name is specified in the argument. It is same as 
//Thread(null,null,name).
public Thread(ThraedGroup group,Runnable target);
//Creates and allocates a new Thread object.This constructor has the same functionality as 
//Thread(group,target,gName),where gName is newly generated name. This is automatic assignment in the form 
//of "Thread-"+n where n is an integer. This constructor can throw an SecurityException if the current thread 
//can not create a thread in the specified group.
public Thread(ThraedGroup group,Runnable target,String name);
//Creates and allocates a new Thread object.target is the it's run object and name as name specified in the 
//argument.It belong to the ThreadGroup referred by group.If the group is null,the new process belongs to the 
//same group as the Thread(which is creating the new Thread).If the target argument is not null,the run method 
//of the target is called when this Thread is started.If the target method is null, the Thread which is creating 
//this Thread,its run method gets executed. The priority of the newly created Thread is set equals to the priority
//of the Thread creating the new Thread.setPriority() mthod can be used to change the priority.This constructor
// can throw an SecurityException if the current thread
//can not create a thread in the specified group.
public Thread(ThraedGroup group,String name);
//Creates and allocates a new Thread object.. It has the same effect as Thread(group,null,name).
//This constructor can throw an SecurityException if the current thread 
//can not create a thread in the specified group.
//Methods:
public static int activeCount();
public static int enumerate(Thread toArray[]);
public void checkAccess();
public void destroy();
public void interrupted();
public void run();
public void start();
public final void join();
public final void join(long millis);
public final void join(long millis,int nanos);
public final void resume();
public final void setDaemon(boolean on);
public final void setName(String name);
//Assigns a name of the thread as name for a thread before the exution of that thread.This enables us to identify 
//the thread with a name.This is //helpful for debugging multithreaded application.
public final void setPriority(int newPriority);
//This method sets the thread's priority value with the argument passed.
public final void stop();
public final void stop(Throwable obj);
public final void suspend();
public int countStackFrames();
public static Thread currentThread();
//It is a static method that returns the Thread object which is the currently running thread.
public static void dumpStack();
public static boolean interrupted();
public static void sleep(long millis);
public static void sleep(long millis,int nanos);
public static void yield();
public final String getName();
//This function returns the current String value of the thread's name as set by the method setName()
public final int getPriority();
//Returns the thread's current priority
public final ThreadGroup getThreadGroup();
public final boolean isAlive();
public final boolean isDaemon();
//return's true if the thread is a daemon thread, else returns false.
public boolean isInterrupted();
public String toString();

}
 
All About Class Thread in Java All About Class Thread in Java Reviewed by Animesh Chatterjee on September 21, 2018 Rating: 5

No comments:

Powered by Blogger.