• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

qus on threads

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic