Thread Thread(ThreadGroup, Runnable, String) in Java

Thread(ThreadGroup, Runnable, String): This method is available in the java.lang.Thread class of Java.

Syntax:

java.lang.Thread.Thread(ThreadGroup group, Runnable target, String name)

This method takes three arguments. This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, and belongs to the thread group referred to by the group.

Parameters: Three parameters are required for this method.

group: the thread group. If null and there is a security manager, the group is determined by SecurityManager.getThreadGroup(). If there is not a security manager or SecurityManager.getThreadGroup() returns null, the group is set to the current thread's thread group.

target: the object whose run method is invoked when this thread is started. If null, this thread's run method is invoked.

name: the name of the new thread.

Throws:

1. SecurityException - if the current thread cannot create a thread in the specified thread group or cannot override the context class loader methods.

Approach

Java

package com.Thread;

public class Thread7 {
    public static void main(String[] args) {

        Runnable target = new Thread();
        String name = "hello";
        ThreadGroup group = new ThreadGroup(name);
        Thread thread = new Thread(group, target, name);

        System.out.println(thread);
    }
}

Output:

Thread[hello,5,hello]


Some other methods of Thread

Thread()This method allocates a new Thread object.

Thread(Runnable)This method allocates a new Thread object with one argument.

Thread(String)This method allocates a new Thread object with the specified name.

Thread(Runnable, String)This method allocates a new Thread object with the specified runnable and the specified name.

Thread(ThreadGroup, Runnable)This method allocates a new Thread object with the specified ThreadGroup and the specified runnable.

Thread(ThreadGroup, String)This method allocates a new Thread object with the specified ThreadGroup and the specified name.

Thread(ThreadGroup, Runnable, String)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, and belongs to the thread group referred to by the group.

Thread(ThreadGroup, Runnable, String, long)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name, belongs to the thread group referred to by the group, and has the specified stack size.

Thread(ThreadGroup, Runnable, String, long, boolean)This method allocates a new Thread object so that it has targeted as its run object, has the specified name as its name belongs to the thread group referred to by group, has the specified stackSize, and inherits initial values for inheritable thread-local variables if inheritThreadLocals is true.

Thread.activeCount()This method returns an estimate of the number of active threads in the current thread's thread group and its subgroups. 

checkAccess()This method determines if the currently running thread has permission to modify this thread.

Constants Thread Class: Some of the constants of the Thread class, are Thread.MIN_PRIORITY, Thread.NORM_PRIORITY and Thread.MAX_PRIORITY.

Thread.currentThread()This method returns a reference to the currently executing thread object.

Thread.dumpStack()This method prints a stack trace of the current thread to the standard error stream.

Thread.enumerate(Thread[])This method copies into the specified array every active thread in the current thread's thread group and its subgroups.

Thread.getAllStackTraces()This method returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread.

getContextClassLoader()This method returns the context ClassLoader for this thread.

Thread.getDefaultUncaughtExceptionHandler()This method returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. If the returned value is null, there is no default.

getId()This method returns the identifier of this Thread. 

getName()This method returns this thread's name.

getPriority()This method returns this thread's priority.

getStackTrace()This method returns an array of stack trace elements representing the stack dump of this thread.

getState()This method returns the state of this thread.

getThreadGroup()This method returns the thread group to which this thread belongs.

No comments:

Post a Comment