• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Certification4career.com

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.certification4career.com/sun/java/programmer/exam2.htm
Yet another Threading
But its interesting though
Take a look at this code
class MyThread implements Runnable{
public void run(){
System.out.println("Running MyThread");
}
}//end of MyThread
class YourThread extends Thread{
public YourThread(Runnable r){
super(r);
}
public void run(){
System.out.println("Running YourThread");
}
}//end of YourThread
public class Driver{
public static void main(String args []){
MyThread t1= new MyThread();
Thread t2 = new Thread(t1);
t2.start();
}
}//end of class
If you try to run Driver class what will be result?
1.It will output "Running MyThread."
2.It will output "Running YourThread."
3.It will output both "Running MyThread," and "Running
YourThread."
4.It will not run.
Like the author, I selected Choice 1.
But to my suprise the answer is 2.
So We both are wrong!
Now here is my question.
super(r) becomes useless in code?
The runnable object never seems to get executed and it seems to
me overriding takes precedence over threading in Java
Is that so?
Code 2:
public class BussyThread implements Runnable{
public void run(){
for(int i=0;i<10; i++){
i=i-1;
}//end of for loop
}//end of run()
public static void main(String args[]){
BussyThread b1=new BussyThread();
BussyThread b2=new BussyThread();
b1.start();
b2.start()
}
}//end of class
Above class will start two threads b1 and b2. Select True statements for above class.
a.Only b1 thread will get chance to run
b.Only b2 thread will get chance to run
c.Both thread will get chance to run
sharing CPU time
d.Neither of the thread will be able to run.
Answer :a ???
My Answer : d
It wont even compile in the first place
choice d was the closest

Ranchers help me
Ragu


[This message has been edited by Ragu Sivaraman (edited August 21, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu
The code you posted does indeed print "Running MyThread". In the code you posted you dont use the YourThread class at all. The actual example from the site you referenced is:

In this case the class YourThread has overridden the Thread class so even though in the constructor you send super an instance of a runnable object it is the run method of the YourThread class that is executed.
To see it work the way you expected you can just put this line in your YourThread constructor:

that will print "Running MyThread"

For the second set of code you posted I think they must have a typo there or something. You're correct the code wont even compile because there is no start method for the runnable class, and they haven't tried to create any real Thread objects, so yes, given the code they posted it would be D.
hope that helps you out
Dave
SCJP
[This message has been edited by Dave Vick (edited August 21, 2001).]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ragu,
For Code no 2 answer is d. You are right.

Indu

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dave Vick:
[B]Ragu
The code you posted does indeed print "Running MyThread". In the code you posted you dont use the YourThread class at all. The actual example from the site you referenced is:

In this case the class YourThread has overridden the Thread class so even though in the constructor you send super an instance of a runnable object it is the run method of the YourThread class that is executed.
To see it work the way you expected you can just put this line in your YourThread constructor:

that will print "Running MyThread"

For the second set of code you posted I think they must have a typo there or something. You're correct the code wont even compile because there is no start method for the runnable class, and they haven't tried to create any real Thread objects, so yes, given the code they posted it would be D.
hope that helps you out
Dave
SCJP
[This message has been edited by Dave Vick (edited August 21, 2001).][/B]


Yes Dave. I agree
I ended up pasting my test code,not the code from
the website
So its seems overriding precedes threading formality Isn't
But if use new Thread(r).start() both messages will comeout
So the Answer given in the website is not correct based on the
code given
It should be B not A
Thankyou all
 
this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic