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

Answering Strategy

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 814
Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fragment IV will call the run() method as if it is a normal method...
It does not start thread...
ITS JUST A METHOD
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 34
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James and Jolly for your explanation
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic