• 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

Thread tricky questions.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could not understand the logic behind the answers for the following two questions.Please help.
Answers are provided in BOLD.
Source Dan Chisholm's Mock test.
Thanks and Regards,
Appu.

Question


What is the result of attempting to compile and run the program?

a. Prints: A
b. Prints: B
c. Prints: AB
d. Prints: BA
e. Compile-time error
f. Run-time error
g. None of the above

--------------------------------------------------------------------------------

Question


What is the result of attempting to compile and run the program?

a. Prints: T1T1T1
b. Prints: T1T1T2
c. Prints: T1T2T2
d. Prints: T1T2T3
e. Prints: T1T1T3 f. Prints: T1T3T3
g. Compile-time error
h. Run-time error
i. None of the above
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appu, the first one is a fairly simple one. You see when you initialize a thread with a runnable, then something like this happens inside the Thread class



Now when you call start on the Thread instance, the run method of Thread class is called. The run method looks like this



So basically the run method of the concrete Thread class is called who has the responsibility to call the run method of the runnable passed to the Thread constructor. In the example given by you, an instance of A class is created to instantiate the thread. Now when the start method is called, the run method of A class is called. It is the responsibility of run method of A class to call run method of the Runnable object passed to it during initialization. Since it doesn't fulfill that responsibility, so the run method B class is not called. A correct way to implement the run method would have been

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second question is also fairly simple. When you call run on a Thread instance, then a new thread is not started to execute the run method. Instead the run method is called in the current thread. A new thread is started only when you call start on a Thread instance. So in the run method of class B

new A().run();

and

new Thread(new A(),"T2").run();

will both call run method in the thread started in main method

new Thread(new B(),"T1").start();

Only the third call in run method of class B will a new thread be started

new Thread(new A(),"T3").start();

This is why the first two calls return T1 i.e. the name of the thread started in the main method...
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the first example,

new A(new B()).start();

Here we are calling start of class A and to its constructor you are passing B. So A's run mothod will run which prints A.


In the second example,

new Thread(new B(),"T1").start(); //Will call run method of B as follows

new A().run(); // will call run of class A and prints name of current thread T1

new Thread(new A(),"T2").run(); // will call run of A but will not start the new thread of execution(see run is calling directly) so prints the same name name T1
new Thread(new A(),"T3").start(); //will call run of A but will start the new thread of execution and print its name as T3.Hope it clears to you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
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
@ Ankit,
No wonder you got 98%.
I had read similar question before . the explaination given there was not clear. you have explained it wonderfully.


 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, your explanations are so simple and to the point. You explain the most difficult topic with so much ease.....

Great work!!! Kudos 2 you man!!
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! It was my pleasure . Good work by Geeta too who is approaching the 100 mark
 
Appu Mehta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am able to get second question. But 1st question i still have doubts.
As per kathy sierra study guide, we can start a thread by two ways.
1.


2.


So, in ques 2

starts a thread and executes run() method of class B. I got it.

But in ques 1,

should start run of B() because it is the runnable target passed as parameter to a new thread instance A as per point 2 mentioned above from Kathy Sierra.

i am confused .Please help
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Appu both works in same way, just try to think more, and do one thing open the code of Thread class and see its run method.

new Thread(new B(),"T1").start();: for this first run() method of Thread class is called, that run() has some coding that will call B.run() method.

new A(new B()).start();: again first run() method of A class is called, but this run() has no code that is calling B.run() method.

Try to think for these two lines. Ask if any doubt.
 
Appu Mehta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks puneet, Ankit, Geeta and all.
 
look! it's a bird! it's a plane! It's .... a teeny 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