• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

answer of this code

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
usually there should be an answer like "could not be determined"..
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic