File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusing threads" Watch "Confusing threads" New topic
Author

Confusing threads

awad saleh
Greenhorn

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
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3275
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.


500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
awad saleh
Greenhorn

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
Author
Ranch Hand

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

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html

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: 802

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

Regards
Ninad
[ 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 ]

------------------------
Bob
SCJP - 86% - June 11, 2009
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusing threads