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

K&B SelfTest Ques ,chap 9 pg 739

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

I have some doubt regarding answers mentioned to Question 12 in Self test exam chap 9,page 738-739.
Ques says ,how to make this application thread safe.

anser book says : is making methods synchronized.

but what i think aswer should be :
making methods synchronzied
and se stringBffer instead of StringBuilder.
As there can be more than 1 answers.
althouht ,we cannt rely n thread-safe classes but it also helps.


So for SCJP, how we get to knw ,as both are right ansers.or for sure ,they put no of possible ansers.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Just replacing StringBuilder by StringBuffer doesn't make class Logger thread-safe : if multiple threads call concurrently the method log, the data into contents could be (will be !) corrupted. Read again the "Thread-Safe Classes" section from the K&B p714.
If the log method is synchronized, it's make the class thread-safe and using the thread-safe class StringBuffer is useless.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there...

you asked... why mark the method as synchronized when we can use stringBuffer to make thread safe code.....???

Answer:

When we mark the method as synchronized, what happens is,the method is called atmost by one Object/thread at a time and until this thread is done using this particular method, no other thread can enter and use this method... kind of a queue....

in StringBuffer class all the methods are individually marked as synchronized....
now lets imagine a scenario when there are two threads executing simultaneously... each on different methods, but come to a point where they need a method to complete their execution.. creating a deadlock.....

"thread-safe" class will fail....

NOTE:
just because a class is described as "thread-safe, doesn't meant that it is always thread-safe"

.

besides why rely on classes when you can get away by just marking a single method as Synchronized....

if you want more ...read chapter 9: page 714 K&B..carefully...

good luck,.........
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic