This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusing threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusing threads" Watch "Confusing threads" New topic

Confusing threads

awad saleh

Joined: Jul 04, 2007
Posts: 22
hello Friends.....

1>public class Test {
2> public static void main(String[] args) {
3> System.out.print("A ");
4>Thread t=new Thread();//Creating an object of type thread
5> new Thread(t).start();
6> System.out.println("B");

In the above program i am creating an object of type thread .
and in line 5 i m passing the object while creating another object as parameter.
Now my doubt is As there is no construct in thread class that accepts parameter of the type thread. then how is program is getting executed?
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Your main method is running in its own thread and that is why your program is working. The way you are creating an additional thread is incorrect. You need to write your own class either extending Thread or implementing Runnable (preferred approach) interface with the run() method.

Java Interview Questions and Answers Blog | profile | Java Interview Books
awad saleh

Joined: Jul 04, 2007
Posts: 22
i m not satisfied with your answer..

i want to know when there is now constructor in the thread class which accepts the parameter of type Thread . Then how this program is getting executed.......?

I will answer. Correct me if i m wrong.
bartenders please watch this topic and Correct me........

My Ans:-)As There are many overloaded constructors in Thread class and there's one Constructor that accepts one parameter as runnable . And as Thread Class implements runnable so we can pass parameter of type Thread which means a runnable object..

Bartenders please correct if m wrong..
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Check the Java Thread API

Thread class implements the Runnable interface and it has a constructor that takes argument of type Runnable.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

Hi,awad saleh
Thread class implements Runnable and Thread constructor accept Runnable type as parameter so program works well see java API docs for details

Arulk is perfectly correct

[ October 16, 2008: Message edited by: Ninad Kulkarni ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
What has been said is true but the one fact that does not stand out may help you.
1) Thread implements Runnable
2) Implementing Runnable requires a class to implement the method
public void run();
3) the basic Thread class in the API implements an empty run
(public void run() {}; )
4) Since Thread implements Runnable, it passes the IS-A test for a Runnable type so a thread
can be passed in to a paramter that specifies a Runnable.

So what you are doing is instantiating a Thread, passing it to a second Thread as the Runnable parameter (that Runnable is actually a Thread with a null run() method). So, when you call the start() method, the new thread starts up, starts the empty run() method, and, since there is nothing to do, quietly exits. The main thread does all of the printing, and then it dies, and the JVM comes down. Technically it all ran properly, just the new second thread wasn't given anything to do because IT'S run() method was not overridden.

I hope this helps.

[ October 17, 2008: Message edited by: Bob Ruth ]
[ October 17, 2008: Message edited by: Bob Ruth ]

SCJP - 86% - June 11, 2009
It is sorta covered in the JavaRanch Style Guide.
subject: Confusing threads
Similar Threads
Doubt about a mock test question - Generics and static context
Generics Query
Generic methods doubt
code for isAlive()