Java Programming - Threads - Discussion

2. 

Which two are valid constructors for Thread?

  1. Thread(Runnable r, String name)
  2. Thread()
  3. Thread(int priority)
  4. Thread(Runnable r, ThreadGroup g)
  5. Thread(Runnable r, int priority)

[A]. 1 and 3
[B]. 2 and 4
[C]. 1 and 2
[D]. 2 and 5

Answer: Option C

Explanation:

(1) and (2) are both valid constructors for Thread.

(3), (4), and (5) are not legal Thread constructors, although (4) is close. If you reverse the arguments in (4), you'd have a valid constructor.


Sudha said: (May 20, 2011)  
Why 4th one is wrong, what is the main raeson.

I mean wether its syntactical error or else what.

Gaffney said: (Jul 29, 2011)  
It is wrong because the order is wrong. If it were Thread (ThreadGroup g, Runnable r) then that's also a valid constructor.

Ashish said: (Jul 31, 2012)  
Why the order of the (4) constructor does matter ?

Ravi Kant Chauhan said: (Jun 12, 2013)  
We can declare a thread constructor in 4 ways.

Thread()
Thread(Runnable target)
Thread(Runnable target, String name)
Thread( String name)

Bishal said: (Oct 30, 2013)  
Hey friends.

We can declare thread in 7 ways:

Thread()
Thread(Runnable target)
Thread(Runnable target, String name)
Thread( String name)
Thread(ThreadGroup g)
Thread(ThreadGroup g,Runnable target)
Thread(ThreadGroup g,Runnable target,String name)

Veevesh said: (Dec 6, 2013)  
We can declare thread in 8 ways:

Thread()

Thread(Runnable target)

Thread(Runnable target, String name)

Thread(String name)

Thread(ThreadGroup group, Runnable target)

Thread(ThreadGroup group, Runnable target, String name)

Thread(ThreadGroup group, Runnable target, String name, long stackSize)

Thread(ThreadGroup group, String name)

Masoud said: (Dec 14, 2014)  
What's the difference between Thread(Runnable r, String name) and Thread(Runnable) ?

Prakash VL said: (Jul 17, 2016)  
If we have implemented the Runnable interface in our thread class then we need to create Thread class object to start our thread, while we creating thread class object we will pass our required class object which already implemented Runnable interface it's kind of polymorphism "a parent type variable can hold its child class object".

Finally Runnable is an interface.

Geetha said: (Nov 11, 2016)  
What is ThreadGroup g? Please, anyone, explain.

Ramya said: (May 16, 2018)  
What is runnable?

Kartiki said: (Sep 6, 2018)  
What is the main use of thread class?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.