qus on threads
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
| Curse your sudden but inevitable betrayal! And this tiny ad too! Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






