This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes answer of this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "answer of this code" Watch "answer of this code" New topic
Author

answer of this code

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
i would like to know what is the answer for the following code, cause in the book the answer is (Wallace-1 Gromit-1 Gromit-2 Wallace-2) but my answer is (Wallace-1 Wallace-2 Gromit-1 Gromit-2) below is the code:
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by ming ming:
i would like to know what is the answer for the following code, cause in the book the answer is (Wallace-1 Gromit-1 Gromit-2 Wallace-2) but my answer is (Wallace-1 Wallace-2 Gromit-1 Gromit-2) below is the code:




Well you are correct..
answer given in the book is not correct..


The Best way to predict your future is to create it - Every great individual common man
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
I believe there are other answers.
It may print out "Wallace-1 Gromit-1 Wallace-2 Gromit-2" b/c it really depends on OS.
[ August 01, 2006: Message edited by: wise owen ]
Flom Xanther
Ranch Hand

Joined: May 26, 2006
Posts: 48
usually there should be an answer like "could not be determined"..
Ted Clark
Greenhorn

Joined: Jul 18, 2006
Posts: 6
I thought I read in the Sierra and Bates SCJP5 book that very little is guaranteed with threads and that included the thread scheduler. Which means I believe that the output from the two threads in the example code can vary.
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
Originally posted by ming ming:
i would like to know what is the answer for the following code, cause in the book the answer is (Wallace-1 Gromit-1 Gromit-2 Wallace-2) but my answer is (Wallace-1 Wallace-2 Gromit-1 Gromit-2) below is the code:


Are you damn sure about the answer that you have got? how about re-running the program once? twice? a million times? On somebody else's machine? on some other OS? With some other JVM version?

Note that the output of your program depends on the scheduling algorithm implemented by JVM - and very little is guaranteed about the result. Typically, the order of the statements printed on the console will vary depending on how the threads are scheduled by the JVM.

Please note that I am not saying that the answer given by the book is correct or something like that, I am just pointing out that the order in which the statements are printed is unpredictable.
Albertas Laurinavicius
Greenhorn

Joined: Jul 09, 2006
Posts: 5
So what is right answer for question: "Which of the following is a possible result? (Choose all that apply.)"
In book given these:

A. Wallace-1 Wallace-2 Gromit-1
B. Wallace-1 Gromit-2 Wallace-2 Gromit-1
C. Wallace-1 Gromit-1 Gromit-2 Wallace-2
D. Gromit-1 Gromit-2
E. Gromit-2 Wallace-1 Gromit-1 Wallace-2
F. The code does not compile.
G. An error occurs at run time.

I think BCE should be correct.


SCJP,SCWCD
Eleanor Leong
Greenhorn

Joined: Mar 24, 2006
Posts: 21
Hi,
I think only C is correct.
Since no matter which messenger, Wallace or Gormit, 1 has to come before 2.
Option A Gromit-2 is missing
Option B Gromit-2 is before Gromit-1
Option D, Wallace-1 and Wallace-2 are missing
Opriont E Gromit-2 is before Gromit-1

What do you think?
Albertas Laurinavicius
Greenhorn

Joined: Jul 09, 2006
Posts: 5
I've tested with code(i believe it is 100% like in book):

public class Messager implements Runnable
{
private String name;
/** Creates a new instance of Messager */
public Messager(String name)
{
this.name=name;
}
public void run()
{
message(1);
message(2);
}
private synchronized void message(int n)
{
System.out.print(name+"-"+n+" ");
}
public static void main(String[] s)
{
new Thread(new Messager("Wallace")).start();
new Thread(new Messager("Gromit")).start();
}
}

and i've got: Wallace-1 Wallace-2 Gromit-1 Gromit-2
But i think there is theoretical chance to get BCE answers.
Dave Reinhardt
Ranch Hand

Joined: Aug 07, 2006
Posts: 54
I think the book is correct. The question doesn't ask what is the correct answer, it asks which are _possible_ correct answers. C is the only possible correct answer as the book and Eleanor said. Your output is also correct, but that answer is not listed.

See this part of the code, and the explaination in the book: p 744

public void run(){
message(1);message(2);
}

The only way to get 2 before 1 with a single name would be to reverse those numbers in the run() method

possible correct answers are:
w1 w2 g1 g2
g1 g2 w1 w2
w1 g1 w2 g2
g1 w1 g2 w2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: answer of this code
 
Similar Threads
Problem with Chapter 9 Question 1
thread doubt
Question about threads in Kathy's Book.I don't understand.
thread on different instances?
Threads: synchronized method (K&B 1.5, page 733)