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

qus on threads

narra sivakanth

Joined: Nov 02, 2000
Posts: 7
Check this code it is compiling and running fine
1.class th extends Thread
3.public static void main(String args[])
4.{ A =new th();
6.Thread t1 = new Thread(A);
7.Thread t2 = new Thread(A);
10.public void run()
11.{ x=5,y=5;
13.for(int i =0 ; i<5;i++)
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

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
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
A.start(); // A is an object of th which extends Thread


Thanks & Regards,<br />Nijeesh.
I agree. Here's the link:
subject: qus on threads
It's not a secret anymore!