• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

doubts on questions #12, #13, #15, #16 and #17 self-test Chapter 13

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have great difficulty in understanding the operation of the code supplied with the exercises mentioned.

#12
Given this code:


Which letters will eventually appear somewhere in the output?


Correct answers:
A. A
C. C
D. D
E. E
F. F



#13
Given this code:

and given the following five fragments:


I. new Starter().run();
II. new Starter().start();
III. new Thread(new Starter());
IV. new Thread(new Starter()).run();
V. new Thread(new Starter()).start();



When the five fragments are inserted, one at a time at line 9, which are true?


Correct answers:
C. Only one might produce the output 4 2
D. Exactly two might produce the output 4 4



#15
Given this code:

And given these two fragments:


I. synchronized void chat(long id) {
II. void chat(long id) {



When fragment I or fragment II is inserted at line 5, which are true?


Correct answer:
F. With fragment II, the output could be yo dude dude yo



#16
Given this code:

Which are true?


Correct answer:
F. An exception is thrown at runtime




#17
Given this code:

And given these two fragments:


I. synchronized void move(long id) {
II. void move(long id) {



When either fragment I or fragment II is inserted at line 7, which are true?


Correct answers:
C. With fragment I, the output could be 4 2 4 2
E. With fragment II, the output could be 2 4 2 4





I just can not understand why the correct answers are those. Can you help me understand?
I apologize for the amount but I just can not understand.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going back to apologize for the length but I have other 3 codes (taken from the questions on cd) of which I can not understand the operation.

-
Given this code:

We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any other combination. Which method definitions could be added to the Letters class to make this guarantee?


Correct answers:
E. public void run() { synchronized(Letters.class) { write(); } }
F. public void run() { synchronized(System.out) { write(); } }





-
Given this code:

What is the result?


Correct answer:
A. Ren Ren Ren... ...Stimpy Stimpy Stimpy





-
Given that the following code is intended to create a single WidgetMaker that makes Widget objects and several WidgetUsers who wait for a Widget to be made:

If each WidgetUser should receive its own Widget, which no other WidgetUser is using, what code should be inserted in place of the comment // INSERT SOMETHING HERE ?


Correct answer:
C.
synchronized(finishedWidgets) {
finishedWidgets.add(w);
finishedWidgets.notify();
}

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone help me?
 
author
Posts: 23959
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

John Lerry wrote:anyone help me?



There are reasons that long posts don't get much attention, and apologizing generally doesn't get it more attention. Also, you need to ShowSomeEffort with each question, and not just have all your effort in cutting and pasting lots of questions.

Are any of these questions a priority? If so, pick one, show us that one question, explain us the answer you think it is, why you think so. And then we can give you a hint in the right direction.

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're right. I try to give an answer to the first question.

John Lerry wrote:
#12
Given this code:


Which letters will eventually appear somewhere in the output?


Correct answers:
A. A
C. C
D. D
E. E
F. F





First you can not determine the order of output because there is no way to establish which of the two threads to run first, in any case, the thread "laure1" will print "A" and "C", with one second between the two results, while thread "hardy" will print "D", then "E" (the exception is thrown because there is NOT a method notify()) and then "F".

Is it correct?
 
Henry Wong
author
Posts: 23959
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

John Lerry wrote:
First you can not determine the order of output because there is no way to establish which of the two threads to run first, in any case, the thread "laure1" will print "A" and "C", with one second between the two results, while thread "hardy" will print "D", then "E" (the exception is thrown because there is NOT a method notify()) and then "F".

Is it correct?



Can you point to us a source/link that says that an exception will be thrown when you call wait() and no other thread calls notify()? If so, how do you think that the environment can tell that no notification will arrive in the future?

Henry
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually it makes no sense. The wait() method MUST be enclosed within a try/catch block to not generate an error in compilation but I can not understand because in this performance an exception would be thrown.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic