Thread class in Java | Thread Constructor and Methods

 Thread class in Java | Thread Constructor

A Thread class is a thread of execution in a program. The JVM allows an application to have multiple threads of execution running concurrently. 

 

Thread Constructor 

Thread t =  new Thread()

Create a new child thread. Thread name automatically generate in the form of Thread-n (where n is the integer number)
 

Thread t =  new Thread(Runnable target)

Create a new child thread. target is object, whose run method is invoke  when this thread is started. If target is null then this class method does nothing

Thread t =  new Thread(String name)

Create a new child thread. Name of this thread is name. 

 Thread t =  new Thread(Runnable target, String name) 

Create a new child thread. Name of this thread is name. 

target is object, whose run method is invoke  when this thread is started. If target is null then this class method does nothing.
 

Thread t =  new Thread(ThreadGroup group, String name)

Create a new child thread. Name of this thread is name. 
The group is set to the current thread's thread group.
 

Thread t =  new Thread(ThreadGroup group, Runnable target)

Create a new child thread. Thread name automatically generate in the form of Thread-n (where n is the integer number). 
 
The group is set to the current thread's thread group.  
 
target is object, whose run method is invoke  when this thread is started. If target is null then this class method does nothing.
 

Thread t =  new Thread(ThreadGroup group, Runnable target, String name)

Create a new child thread. Name of this thread is name. 

The group is set to the current thread's thread group. 

target is object, whose run method is invoke  when this thread is started. If target is null then this class method does nothing. 

 

Thread t =  new Thread(ThreadGroup group, Runnable target, String name, long stackSize )

Create a new child thread. Name of this thread is name. 

The group is set to the current thread's thread group. 

target is object, whose run method is invoke  when this thread is started. If target is null then this class method does nothing. 

stackSize, the desire stack size for the new thread, or zero to indicate that this parameter is to be ignored. 

 

Thread Methods

public static int activeCount()     

Return an estimation of the active thread in curent thread's and it's subgroup and in any other thread group that has current thread's thread group as an ancestor . Value returned by it's method is only an estimate, because threads may be changed dynamically while this method traverses internal data structure.

public static native Thread currentThread()

 Returns currently executing thread object's reference.
 

public static void dumpStack()

Print a stack trace of the current thread. This method is used only for debugging purpose.  

public static int enumerate(Thread tarray[])

Copies into the specified array every active thread in the current thread's thread group and its subgroups.

If the array is too short to hold all the threads, the extra threads are silently ignored.

Due to the inherent race condition in this method, it is recommended that the method only be used for debugging and monitoring purposes. 

Return the number of threads put into the array. 

 

public static Map<Thread, StackTraceElement[]> getAllStackTraces()

Returns a map of stack traces for all live threads.  

public static native boolean holdsLock(Object obj)

Return true if the current thread holds the monitor lock on the specified object obj.  

public static boolean interrupted()

Return true if the current thread has been interrepted otherwise return false.

public boolean isInterrupted()

Return true if the this thread has been interrepted otherwise return false. 

public final void join()

Waits for this thread to die. 
An invocation of this method behaves in exactly the same way as the invocation 
 

public final synchronized void join(long millis)

Wait a period of time (in miliseconds) for this thread to die. If timeout means wait is over and thread die immediately.

public final synchronized void join(long millis, int nanos)

Wait a period of time (in miliseconds and nanos ) for this thread to die. If timeout means wait is over and thread die immediately.

public void run()

This is inherit from Runnable interface. This method called when Thread's start() method called.

public void setContextClassLoader(ClassLoader cl)

Set the context ClassLoader for this Thread. The context ClassLoader can be set when a thread is created  

public final void setDaemon(boolean on)

Set this thread is a demon thread. This method must be called before the thread is started. 

public final synchronized void setName(String name)

Update the name of current thread with provided parameter  

public final void setPriority(int newPriority)

Update or set the new priority to the current thread with provided parameter.
Priority should be between 1 to 10. 
 

public synchronized void start()

This method is responsible to start a thread via calling start() method. After called this method JVM called the run() method of this thread.  

 

public String toString()

Return a string including thread's name, priority and thread group.  

public static native void sleep(long millis)

Current thread sleep for the specified times in milliseconds.

public static void sleep(long millis, int nanos)

Current thread sleep for the specified times in milliseconds and nanosecond 

public static native void yield()

This method gives hint to the scheduler that current thread is willing to yield its current use of a processor. 
Scheduler may or may not ( free to ) ignore this hint.
It is useful for debugging concurrency control constructs.
 

public long getId()  

A positive long unique ID is generated when thread is created. 
This method return those thread ID. 
 

public final String getName()

Return the name of the calling thread's. 

public final int getPriority()

Return the priority of the calling thread's.

public State getState() 

Return the state of the calling thread's.

public final ThreadGroup getThreadGroup()

Return the thread group which this thread belongs.  

public void interrupt()

interrupt the thread. 

public final native boolean isAlive() 

Return true if this thread is alive, otherwise return false.

A thread is alive when it has been started and not yet die.

public final boolean isDaemon()

Return true if this thread is a demon thread.

 

 Related Post

 

RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post