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 qus on 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 "qus on threads" Watch "qus on threads" New topic
Author

qus on threads

narra sivakanth
Greenhorn

Joined: Nov 02, 2000
Posts: 7
Check this code it is compiling and running fine
1.class th extends Thread
2.{
3.public static void main(String args[])
4.{
5.th A =new th();
6.Thread t1 = new Thread(A);
7.Thread t2 = new Thread(A);
8.t1.start();
9.}
10.public void run()
11.{
12.int x=5,y=5;
13.for(int i =0 ; i<5;i++)
15.{
16.System.out.println(x+""+y);
17.}
18.}
19.}
At line 5 we are constructing a thread which is referred by A (as th is subclass of Thread so A is a Thread).
At line 6 and 7 we are passing this Thread A as argument to constructor's of t1 and t2,as for I know there is no constructor in the thread class which takes a Thread a parameter.
But this code is compiling and running also.
Can any body explain me how?
And one more thing this code of 19 lines, if I add two more methods to this app then it will become more that 25 line, is this is the way sun ask 25+ lines of code in the Real exam.
Will only a brace can be in a line as in above code (lines 2,4,9,7 etc)?
Who took the Real Exam please help me.
Every body (who cleared the scjp) are saying that Threads are tough tough tough what they me by though, is sun asking questions in a tricky way or code is lengthy i.e. 25+ lines or some thing else. Acutely what they mean by saying tough, please help me?
Vasu n
Greenhorn

Joined: Nov 11, 2000
Posts: 2
I believe it's using Thread(Runnable) constructor..
The Thread subclass is assaigned to the Runnable interface which is legal.
If you add following code in between the lines 5 and 6:
Runnable r = A;
the code compiles and you can directly pass the above reference r to the Thread instead of A.
Thread t1 = new Thread(r);
Still the outcome will be same.

Nijeesh Balan
Ranch Hand

Joined: Oct 09, 2000
Posts: 116
Hi,
Is this because that Thread class implements the Runnable interface??
One more..
Can somebody tell me the difference between..
t1.start(); //t1 is an object of thread
and
A.start(); // A is an object of th which extends Thread

Thanks
Nijeesh.


Thanks & Regards,<br />Nijeesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: qus on threads
 
Similar Threads
Why Both Threads Printing their own variable x value.
Output even more unexpected than I was expecting!
Threads and Synchronization examples
join() method in Thread doesn't work as expected