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()
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.
Thread t = new Thread(ThreadGroup group, String name)
Thread t = new Thread(ThreadGroup group, Runnable target)
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()
public static native Thread currentThread()
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.
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()
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)
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.