aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Answering Strategy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Answering Strategy" Watch "Answering Strategy" New topic
Author

Answering Strategy

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

This question is from KS book...


With these kind of question , i consume lot of time.....
I usually do by taking one fragment and go through all the option and take the second fragment and go through all the option and so on...

Is there any other way of sloving these kind of questions...
[ December 02, 2008: Message edited by: James Tharakan ]

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

I usually do by taking one fragment and go through all the option and take the second fragment and go through all the option and so on...


I think that's the way you should check the answers. But you may not need to go through all the answers for some options as it may obvious the output hence the correct answer.
[ December 02, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

Originally posted by James Tharakan:
This question is from KS book...


With these kind of question , i consume lot of time.....
I usually do by taking one fragment and go through all the option and take the second fragment and go through all the option and so on...

Is there any other way of sloving these kind of questions...

[ December 02, 2008: Message edited by: James Tharakan ]


I think the answer is option D and E

only Fragment I and Fragment V will produce output

By using Fragment I Thread Id values are same

By using Fragment V Thread Id values are different one for main thread and another one for started thread

Fragment II will produce error
Fregament III will create Thread object with starter Runnable
and Fragment IV will call run() method of Thread object

I hope this clears and notify me If I made mistake somewhere


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34

I think the answer is C & D
I tried running the code and found that
Fragment I ,IV and Fragment V are giving output

Fragment V prints different id's one for main n other one for the new thread started.

By using Fragment I & IV Thread Id values are coming same
Can some tell why the fragment IV is printing the same id though we are printing the id of the newly created thread in the go() method.
new Thread(new Starter()).run();

Is it so that by calling the run method through .run() is not having the same effect as calling the .start().

and what is the significance of calling the run method directly.

Thanks in Advance


SCJP6:80%, OCWCD 88 %
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Thankyou for the last two post's ranchers...
I was actually asking about the answering strategy and not the solution of the problem....

never mind
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

Hello Shweta S Sharma
You are perfectly correct
I was wrong the answer is C and D

I know Runnable's run method will run when thread object is constructed with runnable
but I don't know when thread object is constructed with runnable and calling run method directly not by starting separate thread which run the runnable's run
how run method of starter executed when thread object directly calls run method thread object
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ Shweta S Sharma S
Here is the output of each fragment.
I-4 4
II-Error
III- 4
IV-4 4
V- 4 2
So answer is C & D

Fragment IV prints the same output because, it starts a new thread but it is calling run() method directly.It is not running the thread.It would have started the thread only if it calls the start() method.
[ December 02, 2008: Message edited by: James Tharakan ]
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34



Fragment IV prints the same output because, it starts a new thread but it is calling run() method directly.It is not running the thread.It would have started the thread only if it calls the start() method.


I am still little confused with the output of Fragment 4
Does calling the run method directly starts a new thread or calling run method directly through .run() just takes it as a normal method and executes its body.
Can someone throw more light on this

Thanks in Advance
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Fragment IV will call the run() method as if it is a normal method...
It does not start thread...
ITS JUST A METHOD
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
@shweta
i would like to refer code snippet from java.lang.Thread class to clarify it.

public void run()
{
if(target!=null){
target.run()
}

}

and in the context of discussion you can see
IV. new Thread(new Starter()).run();, so target is available and invocation of run() on thread object will invoke its (Runnables) run method.
And Regarding the creation of new Thread its same that a new thread is created only by invoking start() on that Thread instance.
and that is why the Ids which you get in the Output are same.

Hope this is will help you.
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34

Thanks James and Jolly for your explanation
 
Don't get me started about those stupid light bulbs.
 
subject: Answering Strategy