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 1 of 2.

Anuj Tyagi said:   1 decade ago
Here is the best explanation:
If multi threading is being implemented with Runnable interface then a new thread object is created in below fashion

Thread t = new Thread(this);

Here this pointer is passed as the self reference
In place of this we can pass the object of class X. i.e

X run = new X()

Now we can replace this with run and a new thread will be created like

X run = new X();
Thread t = new Thread(run);
t.start();
(1)

Tulcram said:   1 decade ago
What does the X stands..?

Kaushiki singh said:   1 decade ago
I m also in doubt with this answer I think B option should be the right answer. Can you please give the explaination for this answer.

Sundar said:   1 decade ago
@All

The given answer is correct. Don't get confused.

A. Thread t = new Thread(X);

Wrong. Because, here X is the ClassName not an object.

B. Thread t = new Thread(X); t.start();

Wrong. Because, here X is the ClassName not an object. So thread cannot be started. It would have been correct if it was written like:
Thread t = new Thread(new X()); t.start();

D. Thread t = new Thread(); x.run();

Wrong. Here, object 'x' doesn't make any sense.


But, Option C: is correct

X run = new X(); //Creates objects that implements run() as per Runnable interface

Thread t = new Thread(run); // Creates thread

t.start(); // Starts the thread.


Note: The above three statements can be written as simply

(new Thread(new X()).start();

Hope this help you. Have a nice day!

PRADEEP said:   1 decade ago
The process of working with a thread

1. create an object as here
x run= new x();
2. create a thread and attach an object as here
thread t = new thread(run);
3. start the thread
t.start();

Gopichand said:   1 decade ago
I got confused, how it's work please explain it detail.

Manivannan.k said:   1 decade ago
Ya its correct. Whatever class used in java we must used called the object to a responsible for the class so after thread class created then thread will be run in java programs.

Sri rajyalakshmi said:   1 decade ago
Actually I am also confused thanks for clearing this.

Vk007 said:   1 decade ago
Hi guys,

The given answer is correct and there is nothing confusing cause:.

1. Thread need to know where it has to run so by passing object of that class we convey this.

2. To call run method, we call start method of thread class.

Jailalita Gautam said:   1 decade ago
Firstly we have to create object of the Class that implements the Runnable interface.
Like: X obj= new X();

Pass this object to the thread class that starts the main job.
Thread thread= new Thread(obj);
thread.start();


Post your comments here:

Your comments will be displayed after verification.