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 ?Answer: Option
Explanation:
Option C is suitable to start a thread.
Discussion:
15 comments Page 1 of 2.
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!
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!
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();
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();
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.
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.
MohanRao said:
9 years 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.
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();
Like: X obj= new X();
Pass this object to the thread class that starts the main job.
Thread thread= new Thread(obj);
thread.start();
PreethySuresh said:
9 years 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?
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?
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();
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();
Chetan pattar said:
6 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.
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.
Satish chavan said:
9 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers