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.
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.
Rayon said:
7 years ago
You are correct @Sundar. Agree with you.
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.
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.
PreethySuresh said:
10 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?
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();
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();
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.
Sri rajyalakshmi said:
1 decade ago
Actually I am also confused thanks for clearing this.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers