Thread Thread(Runnable, String) in Java

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

Syntax:

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

This method takes two arguments. This method allocates a new Thread object with the specified runnable and the specified name.

Parameters: Two parameters are required for this method.

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.

Exceptions: NA

Approach

Java

package com.Thread;

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

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

        System.out.println(thread);
    }
}

Output:

Thread[hello,5,main]


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