IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

Java Programming - Threads - Discussion

@ : Home > Java Programming > Threads > General Questions - Discussion

Read more:

"Success has many fathers, while failure is an orphan."
- (Proverb)
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 ?

[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(); x.run();

Answer: Option D

Explanation:

Option C is suitable to start a thread.


Tulcram said: (Mon, Feb 7, 2011 01:28:56 AM)    
 
What does the X stands..?

Kaushiki Singh said: (Mon, Mar 7, 2011 02:11:31 AM)    
 
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: (Mon, Mar 7, 2011 02:58:51 AM)    
 
@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: (Sun, Apr 24, 2011 06:40:45 AM)    
 
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: (Thu, May 12, 2011 01:00:32 PM)    
 
I got confused, how it's work please explain it detail.

Manivannan.K said: (Tue, Sep 6, 2011 10:49:35 PM)    
 
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: (Sun, Sep 11, 2011 12:16:13 PM)    
 
Actually I am also confused thanks for clearing this.

Vk007 said: (Thu, Mar 1, 2012 11:28:50 PM)    
 
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: (Tue, Apr 24, 2012 09:53:34 PM)    
 
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();

Write your comments here:
Name *:     Email:


© 2008-2012 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!