Thread Class
- Thread class consists four major elements:
- Nested Class
- Fields
- Constructors
- Methods
1. Nested Class
- static class Thread.State : A thread state.
- static interface Thread.UncaughtExceptionHandler : Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception.
- 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.
- 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.
- activeCount:
- Syntax: static int activeCount()
- Use: Returns an estimate of the number of active threads in the current thread's thread group and its subgroups.
- Returns: an estimate of the number of active threads in the current thread's thread group and in any other thread group that has the current thread's thread group as an ancestor
- Example: System.out.println(" "+ Thread.activeCount());
- currentThread:
- Syntax: public static Thread currentThread()
- Use: Returns a reference to the currently executing thread object.
- Returns: the currently executing thread.
- Example: System.out.println("Thread ID :" + Thread.currentThread());
- destroy: @Deprecated
- Syntax: public void destroy()
- Use: This method was originally designed to destroy this thread without any cleanup.
- Note: However, the method was never implemented.
- getId:
- Syntax: public long getId()
- Use: Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.
- Returns: this thread's ID.
- Example: System.out.println("Thread ID :" + Thread.currentThread().getId());
- getName:
- Syntax: public final String getName()
- Use: Returns this thread's name.
- Returns: this thread's name.
- Example: System.out.println("Thread Name:" + Thread.currentThread().getName());
- getPriority:
- Syntax: public final int getPriority()
- Use: Returns this thread's priority.
- Returns: this thread's priority.
- Example: System.out.println("Thread Priority :" + Thread.currentThread().getPriority());
- getState:
- Syntax: public Thread.State getState()
- Use: Returns the state of this thread. This method is designed for use in monitoring of the system state, not for synchronization control.
- Returns: this thread's state.
- Example: System.out.println("Thread State :" + Thread.currentThread().getState());
- getThreadGroup:
- Syntax: public final ThreadGroup getThreadGroup()
- Use: Returns the thread group to which this thread belongs. This method returns null if this thread has died (been stopped).
- Returns: this thread's thread group.
- isAlive:
- Syntax: public final boolean isAlive()
- Use: Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
- Returns: true if this thread is alive; false otherwise.
- Example: System.out.println("Thread isalive :" + Thread.currentThread().isAlive());
- isDaemon:
- Syntax: public final boolean isDaemon()
- Use: Tests if this thread is a daemon thread.
- Returns: true if this thread is a daemon thread; false otherwise.
- Example: System.out.println("Thread isDaemon :" + Thread.currentThread().isDaemon());
- isInterrupted:
- Syntax: public boolean isInterrupted()
- Use:
- Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.
- A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.
- Returns: true if this thread has been interrupted; false otherwise.
- Example: System.out.println("Thread isInteruppted :" + Thread.currentThread().isInterrupted());
- join:
- Syntax:
- public final void join() throws InterruptedException
- public final void join(long millis) throws InterruptedException
- public final void join(long millis, int nanos) throws InterruptedException
- Use:
- Waits for this thread to die.
- Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
- Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.
- Parameters:
- millis - the time to wait in milliseconds
- nanos - 0-999999 additional nanoseconds to wait
- Throws:
- IllegalArgumentException - if the value of millis is negative, or the value of nanos is not in the range 0-999999
- InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
- run:
- Syntax: public void run()
- Use:
- 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.
- Subclasses of Thread should override this method.
- Example: new Thread(object1).run();
setDaemon: - Syntax: public final void setDaemon(boolean on)
- Use:
- Marks this thread as either a daemon thread or a user thread.
- The Java Virtual Machine exits when the only threads running are all daemon threads.
- This method must be invoked before the thread is started.
- Parameters: on - if true, marks this thread as a daemon thread
- Throws:
- IllegalThreadStateException - if this thread is alive
- SecurityException - if checkAccess() determines that the current thread cannot modify this thread
- Example: Thread.currentThread().setDaemon(true);
setName: - Syntax: public final void setName(String name)
- Use: Changes the name of this thread to be equal to the argument name.
- Parameters: name - the new name for this thread.
- Throws: SecurityException - if the current thread cannot modify this thread.
- Example: Thread.currentThread().setName("Sarthak");
setPriority: - Syntax: public final void setPriority(int newPriority)
- Use:
- Changes the priority of this thread.
- First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.
- Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group.
- Parameters: newPriority - priority to set this thread to
- Throws:
- IllegalArgumentException - If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY.
- SecurityException - if the current thread cannot modify this thread.
- Example: Thread.currentThread().setPriority(5);
sleep: - Syntax:
- public static void sleep(long millis) throws InterruptedException
- public static void sleep(long millis, int nanos) throws InterruptedException
- Use:
- Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds and/or the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.
- Parameters:
- millis - the length of time to sleep in milliseconds
- nanos - 0-999999 additional nanoseconds to sleep
- Throws:
- IllegalArgumentException - if the value of millis is negative, or the value of nanos is not in the range 0-999999
- InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
- Example:
start: - Syntax: public void start()
- Use:
- Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
- The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
- It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
- Throws: IllegalThreadStateException - if the thread was already started.
- Example: new Thread(object1).start();
yield: - Syntax: public static void yield()
- Use: A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.
- Example: new Thread(object1).yield();
Tags:
Java