Sunday, 20 November 2016

What is Thread Class In Java ?

Previously we have discussed about

Class Thread

All Implemented Interfaces:

Direct Known Subclasses:
ForkJoinWorkerThread

Declaration of Thread:
public class Thread
extends Object
implements Runnable

A thread is an independent path of execution within a program.Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread known as method named main .The Java Virtual Machine continues to execute threads until either of the following occurs:
1) The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
2) All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception.

There are two ways to create a new thread :
1) By extending the Thread Class : Means Define a class that extends Thread class and override its run() method.
2) By Implementing Runnable Interface : Means Define a class that implements Runnable inteface.The Runnable interface has only one method i.e run() method, that class then implements the run() method.

Field Summary of Thread:

Modifier and Type
Description
static int MAX_PRIORITY
The maximum priority that a thread can have.
static int MIN_PRIORITY
The minimum priority that a thread can have.
static int NORM_PRIORITY
The default priority that is assigned to a thread.

Constructor Summary of Thread:

Constructor
Description
Thread()
Allocates a new Thread object.
Thread(Runnable target)
Allocates a new Thread object.
Thread(Runnable target, String name)
Allocates a new Thread object.
Thread(String name)
Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target)
Allocates a new Thread object.
Thread(ThreadGroup group, Runnable target, String name)
Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.
Thread(ThreadGroup group, Runnable target, String name, long stackSize)
Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.
Thread(ThreadGroup group, String name)
Allocates a new Thread object.
      
Method Summary of Thread:

Modifier and Method Name
Description
static int activeCount()
Returns an estimate of the number of active threads in the current thread's thread group and its subgroups.
Void checkAccess()
Determines if the currently running thread has permission to modify this thread.
protected Object clone()
Throws CloneNotSupportedException as a Thread can not be meaningfully cloned.
Int countStackFrames()
Deprecated.
The definition of this call depends on suspend(), which is deprecated. Further, the results of this call were never well-defined.
static Thread currentThread()
Returns a reference to the currently executing thread object.
Void destroy()
Deprecated.
This method was originally designed to destroy this thread without any cleanup.
static void dumpStack()
Prints a stack trace of the current thread to the standard error stream.
static int enumerate(Thread[] tarray)
Copies into the specified array every active thread in the current thread's thread group and its subgroups.
static Map<Thread,StackTraceElement[]> getAllStackTraces()
Returns a map of stack traces for all live threads.
ClassLoader getContextClassLoader()
Returns the context ClassLoader for this Thread.
static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception.
long getId()
Returns the identifier of this Thread.
String getName()
Returns this thread's name.
int getPriority()
Returns this thread's priority.
StackTraceElement[] getStackTrace()
Returns an array of stack trace elements representing the stack dump of this thread.
Thread.State getState()
Returns the state of this thread.
ThreadGroup getThreadGroup()
Returns the thread group to which this thread belongs.
Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Returns the handler invoked when this thread abruptly terminates due to an uncaught exception.
static boolean holdsLock(Object obj)
Returns true if and only if the current thread holds the monitor lock on the specified object.
void interrupt()
Interrupts this thread.
static boolean interrupted()
Tests whether the current thread has been interrupted.
boolean isAlive()
Tests if this thread is alive.
boolean isDaemon()
Tests if this thread is a daemon thread.
boolean isInterrupted()
Tests whether this thread has been interrupted.
void join()
Waits for this thread to die.
void join(long millis)
Waits at most millis milliseconds for this thread to die.
void join(long millis, int nanos)
Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.
void resume()
Deprecated.
This method is used to resume the suspended thread.
void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
void setContextClassLoader(ClassLoader cl)
Sets the context ClassLoader for this Thread.
void setDaemon(boolean on)
Marks this thread as either a daemon thread or a user thread.
static void setDefaultUncaughtExceptionHandler (Thread.UncaughtExceptionHandler eh)
Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.
void setName(String name)
Changes the name of this thread to be equal to the argument name.
void setPriority(int newPriority)
Changes the priority of this thread.
void setUncaughtExceptionHandler (Thread.UncaughtExceptionHandler eh)
Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
static void sleep(long millis)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
static void sleep(long millis, int nanos)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.
void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
void stop()
Deprecated.
This method is used to stop the thread.
void stop(Throwable obj)
Deprecated.
This method is used to stop the thread.
void suspend()
Deprecated.
This method is used to suspend the thread.
String toString()
Returns a string representation of this thread, including the thread's name, priority, and thread group.
static void yield()
A hint to the scheduler that the current thread is willing to yield its current use of a processor.

Example of Thread:

class X extends Thread{

public void run(){
for(int i=1;i<5;i++){
System.out.println("Thread X : "+i);
}
System.out.println("Exit from Thread X");
}
}
class Y extends Thread{

public void run(){
for(int i=1;i<5;i++){
System.out.println("Thread Y : "+i);
}
System.out.println("Exit from Thread Y");
}
}
public class ThreadClassExample {

public static void main(String[] args) {
new X().start();
new Y().start();
}
}
Program Output:
Thread X : 1
Thread X : 2
Thread X : 3
Thread X : 4
Exit from Thread X
Thread Y : 1
Thread Y : 2
Thread Y : 3
Thread Y : 4
Exit from Thread Y


Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment