Which of the following methods will start the thread?

An application that creates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:

  • Provide a Runnable object. The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread constructor, as in the HelloRunnable example:

    public class HelloRunnable implements Runnable {
    
        public void run() {
            System.out.println("Hello from a thread!");
        }
    
        public static void main(String args[]) {
            (new Thread(new HelloRunnable())).start();
        }
    
    }
    

  • Subclass Thread. The Thread class itself implements Runnable, though its run method does nothing. An application can subclass Thread, providing its own implementation of run, as in the
    public class HelloThread extends Thread {
    
        public void run() {
            System.out.println("Hello from a thread!");
        }
    
        public static void main(String args[]) {
            (new HelloThread()).start();
        }
    
    }
    
    5 example:

    public class HelloThread extends Thread {
    
        public void run() {
            System.out.println("Hello from a thread!");
        }
    
        public static void main(String args[]) {
            (new HelloThread()).start();
        }
    
    }
    

Notice that both examples invoke

public class HelloThread extends Thread {

    public void run() {
        System.out.println("Hello from a thread!");
    }

    public static void main(String args[]) {
        (new HelloThread()).start();
    }

}
6 in order to start the new thread.

Which of these idioms should you use? The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread. The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread. This lesson focuses on the first approach, which separates the Runnable task from the Thread object that executes the task. Not only is this approach more flexible, but it is applicable to the high-level thread management APIs covered later.

The Thread class defines a number of methods useful for thread management. These include Thread4 methods, which provide information about, or affect the status of, the thread invoking the method. The other methods are invoked from other threads involved in managing the thread and Thread object. We'll examine some of these methods in the following sections.

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 activeCount()0

Returns a reference to the currently executing thread object.

void activeCount()2

Deprecated. 

This method was originally designed to destroy this thread without any cleanup. Any monitors it held would have remained locked. However, the method was never implemented. If if were to be implemented, it would be deadlock-prone in much the manner of suspend(). If the target thread held a lock protecting a critical system resource when it was destroyed, no thread could ever access this resource again. If another thread ever attempted to lock this resource, deadlock would result. Such deadlocks typically manifest themselves as "frozen" processes. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.

activeCount()4 activeCount()5

Prints a stack trace of the current thread to the standard error stream.

static int activeCount()7

Copies into the specified array every active thread in the current thread's thread group and its subgroups.

activeCount()8 activeCount()9

Returns a map of stack traces for all live threads.

void0 void1

Returns the context ClassLoader for this Thread.

void2 void3

Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception.

void4 void5

Returns the identifier of this Thread.

void6 void7

Returns this thread's name.

int void9

Returns this thread's priority.

checkAccess()0 checkAccess()1

Returns an array of stack trace elements representing the stack dump of this thread.

checkAccess()2 checkAccess()3

Returns the state of this thread.

checkAccess()4 checkAccess()5

Returns the thread group to which this thread belongs.

checkAccess()6 checkAccess()7

Returns the handler invoked when this thread abruptly terminates due to an uncaught exception.

checkAccess()8 checkAccess()9

Returns true if and only if the current thread holds the monitor lock on the specified object.

void protected Object1 checkAccess()8 protected Object3

Tests whether the current thread has been interrupted.

protected Object4 protected Object5

Tests if this thread is alive.

protected Object4 protected Object7

Tests if this thread is a daemon thread.

protected Object4 protected Object9

Tests whether this thread has been interrupted.

void clone()1

Waits for this thread to die.

void clone()3

Waits at most clone()4 milliseconds for this thread to die.

void clone()6

Waits at most clone()4 milliseconds plus clone()8 nanoseconds for this thread to die.

void int0 void int2

If this thread was constructed using a separate int3 run object, then that int3 object's int5 method is called; otherwise, this method does nothing and returns.

void int7

Sets the context ClassLoader for this Thread.

void int9

Marks this thread as either a daemon thread or a user thread.

activeCount()4 countStackFrames()1

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 countStackFrames()3

Changes the name of this thread to be equal to the argument countStackFrames()4.

void countStackFrames()6

Changes the priority of this thread.

void countStackFrames()8

Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

activeCount()4 suspend()0

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.

activeCount()4 suspend()2

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 suspend()4

Causes this thread to begin execution; the Java Virtual Machine calls the int5 method of this thread.

void suspend()7

Deprecated. 

This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked suspend()8 exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of suspend()9 should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. If the target thread waits for long periods (on a condition variable, for example), the static Thread0 method should be used to interrupt the wait. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.

void static Thread2

Deprecated. 

This method is inherently unsafe. See static Thread3 for details. An additional danger of this method is that it may be used to generate exceptions that the target thread is unprepared to handle (including checked exceptions that the thread could not possibly throw, were it not for this method). For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.

void static Thread5

Deprecated. 

This method has been deprecated, as it is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling static Thread6, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.

void6 static Thread8

Returns a string representation of this thread, including the thread's name, priority, and thread group.

What is the start () and run () method of thread class?

start method of thread class is implemented as when it is called a new Thread is created and code inside run() method is executed in that new Thread. While if run method is executed directly than no new Thread is created and code inside run() will execute on current Thread and no multi-threading will take place.

Which is the correct way to start a new thread Mcq?

On calling Thread start () method a new thread get created. Thread run () method can also be called directly to create thread.

Why thread is called Start method?

The purpose of start() is to create a separate call stack for the thread. A separate call stack is created by it, and then run() is called by JVM. Let us see what happens if we don't call start() and rather call run() directly.