This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads scjp310-065 excercise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads scjp310-065 excercise" Watch "Threads scjp310-065 excercise" New topic
Author

Threads scjp310-065 excercise

suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
Hi,
I am preparing for 310-065 . I am not understanding what is 4 2 & 4 4 in the answer given below .The exercise is

1)
and given the following five fragments:
1. new Starter().run();
2. new Starter().start();
3. new Thread(new Starter()).run();
4. new Thread(new Starter());
5. new Thread(new Starter()).start();// i think this should be inserted

When the 5 fragments are inserted one at time at line 9 which are true?(choose all that apply)
The answer given is Only one might produce the output 4 2 and
Exactly two might produce the output 4 4 //what is 4 2 & 4 4 , how is it generated
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Welcome to JavaRanch.

When you copy a question from a book, mock exam or other source we require that you QuoteYourSources - please tell us where exactly you copied this question from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
SCJP by Kathy Sierra / Bert Bates chapter 9 excercise no 13. similar type of question is exercise no 16 & 17
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
The numbers are the ids of the thread. Have a look at the Thread javadoc to see how they are generated.


Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
suresh krishna Avanthkker wrote: 5. new Thread(new Starter()).start();// i think this should be inserted


The question is not about which one should be inserted, but what each of those lines might produce as output if they were inserted individually.
suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
where can i look for thread javadoc for my knowledge and is it there for scjp 6 exam as it is not mentioned anywhere in the book

when i executed on my system the output is 1 1 or 1 or 1 1 or 1 8 never 4 2 or 4 4
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
I thought a link to the Javadoc would be added automagically when I typed Thread, but it appears it wasn't.

You can find the main Javadoc here and the Thread javadoc is linked to from the left hand pane.

Once you've read that it should be obvious why the questions say 'might produce the output'
Sachin Deokar
Ranch Hand

Joined: May 09, 2008
Posts: 41
Hope this helps.


Prints 1 1
First "1" is the id for Main thread.
Second "1" is the Id for Main thread which is passed as argument to go(1) in run method which is executed as normal method call.

Compilation error. There is no start() method in Starter class

Prints 1 1
same explanation as 1. Calling run() directly just executes the code synchronously (in the same thread), just like a normal method call.
System.out prints the id for Main thread

Prints 1. A new Thread is created but never started (no call to start method)

Prints 1 8
"1" is the id for Main thread.
"8" is the id for new Thread that runs as a result of the call to start method.
suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
the answer for the above excercise is 4 2 & 4 4 but output on my system is 1 1 or 1 or 1 1 or 1 8

Hi, Joanne Neal I went through the javadoc but I was not able to find any rule regarding generation of id nos , is the answer in the book ie 4 2 & 4 4 wrong then.

Hi Sachin, if the threads are numbered as and when it starts , there are maximum 3 - 4 threads generated by us in the above program then how does thread id get a value of 8.(the answer as per the book is 4 2 & 4 4 so is it wrong)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
suresh krishna Avanthkker wrote: Hi, Joanne Neal I went through the javadoc but I was not able to find any rule regarding generation of id nos , is the answer in the book ie 4 2 & 4 4 wrong then.


As I said in my previous post - that's why the questions use the word 'might'. There is no fixed way to generate thread ids (I'm assuming. I haven't actually checked the Java Language Specification), so the ids could be 4 and 2 or they could be 1 and 8 or they could be any other values. A new thread can be given any id that is not currently in use.
suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
On what basis can i answer the above exercise if i get it for the scjp exam , the option given is
a) all five will compile
b) only one might produce the output 4 4
c) only one might produce the output 4 2
d) Exactly two might produce the output 4 4
e) Exactly two might produce the output 4 2
f) Exactly three might produce the output 4 4
g) Exactly three might produce the output 4 2
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
Sachin has already given you the answer to what output you might expect so what do you think the answer is ?
suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
Thank you Joanne Neal and Sachin , but I hope I dont get this type of question while I take up scjp . I did understand what you and sachin said, but if the options are out of the world , I think I have to bet on my stars and guess on the options.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Let's try this again

The JVM assigns each thread a thread ID, which is a number. When you start a new thread, the JVM will assign it a number - you can't predict what the number will be.

>>> The important point is that each thread gets its own unique number.

So in this kind of question, you're really trying to figure out how two (or sometimes more), threads *might* interact with each other. This also means that for this type of question you need to know ways in which the threads CANNOT interact with each other.

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
suresh krishna Avanthkker
Greenhorn

Joined: May 24, 2010
Posts: 14
Dear Bert
I am overwhelmed with joy to get my doubts sorted out from you. Your book is just very very good.
I am science graduate , but for the last 10 years was in finance as an agent for home loans .I had done C reading through Dennis M.Ritchie and Design Of Operating System by Maurice J. Bach around the year 2001 . After a gap of around 10 years ,8 months back when I decided I have to change my profession, my freind Mahesh adviced me to take SCJP 6 exams and bought your book and gave it to me and I have worked out and read your book twice and reading it thrice and each time I go through it , I am getting to know more.I will be taking up SCJP exams by next month end. My Regards to both you and Kathy Sierra for giving such a wonderfull book .

The doubt I had is as given below

1)public class Starter implements Runnable
2) {
3) void go(long id)
4) {
5) System.out.println(id) ;
6) }
7)
8) public static void main(String[] args)
9) {
10) System.out.print(Thread.currentThread().getId()+" ");
11) // insert code here
12) }
13) public void run()
14) {
15) go(Thread.currentThread().getId());
16) }
17) }
and given the following five fragments:
1. new Starter().run();
2. new Starter().start();
3. new Thread(new Starter()).run();
4. new Thread(new Starter());
5. new Thread(new Starter()).start();

As per your advice I have to just assume or work on how many threads are generated when I insert the code at line 11 and since the options given are

a) all five will compile
b) only one might produce the output 4 4
c) only one might produce the output 4 2
d) Exactly two might produce the output 4 4
e) Exactly two might produce the output 4 2
f) Exactly three might produce the output 4 4
g) Exactly three might produce the output 4 2

I should assume the output for line 10(System.out.print(Thread.currentThread().getId()+" ");), ie id for main as 4 and then work as to how many threads are created if I insert the code at line 11 for example if i insert "new Starter().run();" then the argument of go() call will be 4 and the go() method prints out 4 so the answer can be 4 4.So for every code I insert at line 11 , I have to work out the Id numbering like this

Regards
suresh
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi Suresh,

On the real exam it's very common to encounter questions in which, one way or another, you'll be asked to determine what would happen when you:

// insert code here

In this question, you have to work out 5 variations of the code, one for each of the fragments.

Then, for each fragment you have to decide:

- will the code compile?
- how many threads will be created by this code?
- if more than one thread, will the two invocations of S.O.P be from different threads?

For instance, since each thread gets a unique ID, if you think that one of the fragments will mean the code creates only one thread, then, in that case, it wouldn't be possible to see two different thread IDs in the output.

So, as indicated earlier, there is nothing magical about the numbers '2' and '4'. What's important is really whether there is only one ID or two IDs.

hth,

Bert
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads scjp310-065 excercise