• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Solanki
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone please answer the question?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooooooops sorry for the typos and please ignore the last line - I forgot to delete it after doing a copy paste
-sampaths77
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic