Java Programming - Threads - Discussion


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 ?

[A]. Thread t = new Thread(X);
[B]. Thread t = new Thread(X); t.start();
[C]. X run = new X(); Thread t = new Thread(run); t.start();
[D]. Thread t = new Thread();;

Answer: Option C


Option C is suitable to start a thread.

Tulcram said: (Feb 7, 2011)  
What does the X stands..?

Kaushiki Singh said: (Mar 7, 2011)  
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: (Mar 7, 2011)  

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();;

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: (Apr 24, 2011)  
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

Gopichand said: (May 12, 2011)  
I got confused, how it's work please explain it detail.

Manivannan.k said: (Sep 6, 2011)  
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: (Sep 11, 2011)  
Actually I am also confused thanks for clearing this.

Vk007 said: (Mar 1, 2012)  
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: (Apr 24, 2012)  
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);

Anuj Tyagi said: (Aug 4, 2012)  
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);

PreethySuresh said: (Mar 5, 2014)  
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: (Jun 25, 2014)  
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: (Aug 2, 2014)  
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: (Jan 30, 2017)  
You are correct @Sundar. Agree with you.

Chetan Pattar said: (Nov 4, 2017)  
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:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.