aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Threads" Watch "Threads" New topic
Author

Threads

Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
This is a question from one of the mock exam on net
Given Thread t = new Thread(targetObject);
What test of instanceof does targetObject have to pass for this to be legal?
Select the one right answer.
a. targetObject instanceof Thread
b. targetObject instanceof Object
c. targetObject instanceof Applet
d. targetObject instanceof Runnable
e. targetObject instanceof String
The answer given are (d) and (e). But I feel (a) should also be a valid answer.Following are the constructors of Thread class
a.public Thread(Runnable target)
b.public Thread(ThreadGroup group, Runnable target)
c.public Thread(String name)
In the first constructor target can be a class which implements Runnable.Since Thread class implements Runnable (a) should also be true.
Pls let me know if I'm wrong.

Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
can anyone please answer the question?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
import java.lang.Runnable;
public class Class1 implements Runnable{

public static void main(String args[]) {
Class1 c = new Class1();
}

Class1() {
Thread t = new Thread(this);
System.out.println( this instanceof Object);
System.out.println( this instanceof Runnable);

/* Following statements gives compilation error !!
System.out.println( this instanceof Thread);
System.out.println( this instanceof String);
*/
}

public void run() { }

}
Output :
True
True
Since the question asks for one right ans I think ans is instance of Runnable.....
Now I got a new question. Why the last 2 statements give compilation error instead of displaying false ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi
I think that option a) is also an answer from the original posting.Try to run the following code
public class Check {
static Thread thread1 =new Thread();
static Thread thread2 = new Thread (thread1);
public static void main(String [] args){
System.out.println(" Thread1 is " + thread1.toString());
System.out.println(" Thread2 is " + thread2.toString());
System.out.println(" Thread2 instance of thread " +
(thread2 instanceof Thread));
System.out.println(" Thread2 instance of runnable " +
( thread2 instanceof Runnable));
}
}
The output is as follows
Thread1 is Thread[Thread-0,5,main]
Thread2 is Thread[Thread-1,5,main]
Thread2 instance of thread true
Thread2 instance of runnable true

Beena
srikrish
Ranch Hand

Joined: Sep 12, 2000
Posts: 63
I agree with snigs about answer a. When you create a thread objet using Thread t1=new Thread() and Thread t2=new Thread(t1), the code compiles and runs fine.
I also share Dilip's concern regarding the instanceof operator. I would expect it to return false but not give a compile error. The error message talks about incompatible types. Could someone explain on this please?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Guys lets remember the following points:
The question is basically asking what are the possible i/p parameters that can be passed to the Thread constructor. Now looking from the option we might be tempted to just choose (a) and (d). But, remember that there is a constructor Thread(String s) (u can refer to the api for details). Therefore, with given options the instanceof operator must return true for
(A) - coz Thread class does implement Runnable interface. So, an instance of Thread class would very well be coverted to Runnable.
i.e Runnable r = new Thread(); So, (A) must give true. Beena, I guess this must clarify your doubt.
(B)& (C) - I guess there is no probs for anyone with it.
(E) - coz u have a constructor Thread(String s). So the expression with instanceof must return True.
Therefore my answers would be (a), (d), (e).
Now Dilip, u r wondering why your last 2 lines of code is giving u probs - well, besides the regular rules about instanceof operator, remember when u have:
(LHS) instanceof (RHS) //////////// LINE 1
Then LHS must be castable to RHS during compile time. If this is not satisfied there woud be no circumstance during runtime under which LINE 1 would be true. Therefore, java first verifies the possiblity of casting LHS to RHS.
Therefore, in your code "this" - i.e an instance of Class1 is neither a subclass or superclass of Thread nor of String class.
So, there is no way you can cast an instance of class1 to Thread or String. So, your last 2 lines of code gives cimpiler error producing an apt error message explaning the scenario.

Hope this helps
Of course, in case anybody disgress please raise it so that all of can be get our fundamentals correct
-sampaths77
(I) About Threads first:
Thread has a constructor: Thread(String S)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ooooooops sorry for the typos and please ignore the last line - I forgot to delete it after doing a copy paste
-sampaths77
 
Consider Paul's rocket mass heater.
 
subject: Threads