• 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

doubt in k&b

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am going to take scjp1.6 exam soon.I have some doubts in K&B book.Actaully I don't understand the output of the below code which is in K&B book. Will anyody explain me?

The question is:

public class Starter implements Runnable{
void go(long id){
System.out.println( id);
}
public static void main (String [] args){
System.out.print(Thread.currentThread().getID() + " " );
// insert code here
}
public void ru(){
go(Thread.currentThread().getID());
}
The fragments are,
1. new Starter.run();
2. new Starter().start();
3. new Thread(new Starter());
4. new Thread(new Starter()).run();
5. new Thread(new Starter()).start();


The answers are,

Only one might product the output 4 2
Exactly two might product the output 4 4


regards,

jaya




 
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
jayalakshmi please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.


First of all lets eliminate option 2 as it will not compile. Third option will not produce any output. Now when you call the run method on a Runnable/Thread object, then a new thread is not started. Lets say that the first println statement at (1) produces 4 as output (i.e. the first println statement in main method). Now 1st and 4th lines will produce the same output as (1) because the Thread.currentThread() in the run method will return the main thread and as such the ID of the thread will be 4 as well. Only option 5 will produce a different output than line (1) as that statement will start a new thread to call the run method and as such the ID of the thread will be different...
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Actually my doubt was, how the numbers 4,4,4, 2 has come? why can't the numbers like 3, 5 are not coming?

There is no loop in the code. So what's the reason?
Today only i saw the reply . sorry for that
please tell me?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jayalakshmi charugundla wrote:Actually my doubt was, how the numbers 4,4,4, 2 has come? why can't the numbers like 3, 5 are not coming?



You basically misunderstood the question -- the two and four are examples. It could be any two different numbers.

Henry
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry will you please explain this in a easy way?
 
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
Jayalakshmi, the values 2 and 4 are just example values taken by the authors. The value can be anything like 539 instead of 2 and 764 instead of 4. It can be anything. What the question really is testing you is that one of the statements will print two different thread ids (since there will be two threads when that code is run) and two statements will print the same thread id two times (since there will be only one thread when that code is run)...
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Now I clearly understood the output.

 
reply
    Bookmark Topic Watch Topic
  • New Topic