Java Programming - Threads - Discussion

Discussion Forum : Threads - General Questions (Q.No. 4)
4.
class X implements Runnable 
{ 
    public static void main(String args[]) 
    {
        /* Missing code? */
    } 
    public void run() {} 
}
Which of the following line of code is suitable to start a thread ?
Thread t = new Thread(X);
Thread t = new Thread(X); t.start();
X run = new X(); Thread t = new Thread(run); t.start();
Thread t = new Thread(); x.run();
Answer: Option
Explanation:

Option C is suitable to start a thread.

Discussion:
15 comments Page 2 of 2.

PreethySuresh said:   1 decade ago
Inside,
public static void main,

1. Class_name obj=new Class_name("Thread_name");
2. new Class-name("Thread_name");

Does 1 and 2 makes the same sense? are they both same?

MohanRao said:   1 decade ago
Execution wise both will be same. After execution of first statement the reference (obj) of the object will alive whereas in second case once execution over the object will not alive as it is not assigned to any reference.

Satish chavan said:   10 years ago
First we create the object of class after that we pass that object to thread class. And finally, we call the start method which is initial method for start thread.

Rayon said:   7 years ago
You are correct @Sundar. Agree with you.

Chetan pattar said:   7 years ago
Correct me, if I am wrong the class x should be runnable i.e. the class x must implement run() method of runnable interface, and then must be given to thread(runnable arg) constructor.


Post your comments here:

Your comments will be displayed after verification.