• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends...

Anybody can explain this code....

class Counter
{
private final String name;
private int coount;
public Counter(String name)
{
this.neme=name;
}
public String getname()
{
return name;
}
public void increment()
{
count++;
}
public int getCount()
{
return count;
}
public void reset()
{
count=0;
}
}


Which 3 changes would be made to adapt this class to be used safely by multiple threads? (choose two )

A. declare reset() using the synchronized keyword.
B. declare getname() using the synchronized keyword.
C. declare getCount() using the synchronized keyword.
D. declare the constructor using the synchronized keyword.
E. declare increment() using the synchronized keyword.


Answer: A, C, E.




Why these answers are correct ?

Why B, D are not correct ?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

Constructors cannot be marked synchronized. Constructors are used to construct objects (but please, don't cite me ).
It is impossible by design that several threads are about to construct around at one object at the same time.

An author of a textbook on java said that "constructors are implicitely synchronized" (Source not shown because the book is in german).

The name variable cannot be altered in this class, only set once and for all in the constructor. When it cannot be altered, there is no need for synchronization.
The getName() method will return the same name for one object for all concurrent threads to be.

The reset() and increment() methods however could be invoked from several threads and you can get concurrency problems.
OK, you may say they both have only one line of code, but even the count++ and count=0; operations are not atomic, and thus several threads could confuse each other.
And the get() method has also to be synchronized, otherwise thread a could have incremented the variable while the get method is running, and you will obtain a number that is one too big.


I'm only curious why the question says: "Choose two".


Yours,
Bu.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhu repala,

Thanks for your post.

I too have some doubts regarding this question:

After seeing your post, I have tried to get "Data Sharing Problem".
But my trial went in vain.






Please make me understand this:
1. In the above example For every MyThread object we are creating seperate Counter. So how that will cause DATA SHARING problem.

EVERY THREAD IS MANIPULATING THINGS ON ITS OWN COUNTER OBJECT(this)




2. My Understanding is : DATA SHARING PROBLEM CAN BE CAUSED IF RESOURCE IS UNIQUE.

like
2.1 we have Queue of requests to get processed.
There we can have syncrhonized methods for The UNIQUE RequestInterface.

2.2 Data base where we having UNIQUE table on that we fetch and update. records.



Please make me clear on this.

[ August 17, 2007: Message edited by: Srinivasan thoyyeti ]
[ August 17, 2007: Message edited by: Srinivasan thoyyeti ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help !!! (regarding previous post doubts)
I am desperately looking for some help.

I hope you won't let me down.

I know marc weber(you are thread guy) you can save me.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I found my mistakes, though nobody replied to my doubt mentioned in previous mails.
I felt it very bad as no bart-ender or shreiff as visited this thread based on threads.

Data shraing problem comes when we share a common objects among all threads.
Here is code Example which can cause DATA sharing problems:


 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivasan thoyyeti posted Today 10:14 (

Hi Ranchers,

I found my mistakes, though nobody replied to my doubt mentioned in previous mails.
I felt it very bad as no bart-ender or shreiff as visited this thread based on threads.



Ok, this post was from today, 10:14 am Central European Time.
Your previous was from yesterday 19:19 Central European Time.

What do you think we shall do on friday night?

Perhaps there are not enough bartenders from your time zone.


Your problem:
String quote = "


2. My Understanding is : DATA SHARING PROBLEM CAN BE CAUSED IF RESOURCE IS UNIQUE.

".toLowerCase();

Yes. Only when several threads share one common object there are problems when code is not synchronized.


But:
I also didn't succeed in producing code that result in data sharing problems using the non-synchronized version of class Counter (same as above).

My trial that failed:

I expected that after some time the a and b variable may become different, but the code just looped and looped...
and produced no output.

I think, that class Counter may not be thread safe on certain systems only.

If anyone has a running demo that shows that any i++ cause a race condition, I would be curios to see it.

One link:
http://www.galileocomputing.de/openbook/javainsel6/javainsel_09_005.htm#mjd14d8522cd3a050bb6d637ff2fa4dbd5
Only look on Listing 9.16 and the bytecode below. The explaining text (german) says that the one-liner i++ goes into several lines in byte code that may be interupted by the scheduler.


Yours,
Bu.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Burkhard Hassel ,

Here I have done changes to your code:
Now out of 300 threads 299 quit as they start.


Output:

Thread-0a=1
Thread-0b=301
Thread-1a=2
Thread-1b=301
Thread-2a=3
Thread-2b=301
Thread-3a=4
Thread-3b=301
Thread-4a=5
Thread-4b=301
Thread-5a=6
Thread-5b=301
Thread-6a=7
Thread-6b=301
Thread-7a=8
Thread-7b=301
Thread-8a=9
Thread-8b=301
Thread-9a=10
Thread-9b=301
Thread-10a=11
Thread-10b=301
Thread-11a=12
Thread-11b=301
Thread-12a=13
Thread-12b=301
Thread-13a=14
Thread-13b=301
Thread-14a=15
Thread-14b=301
Thread-15a=16
Thread-15b=301
Thread-16a=17
Thread-16b=301
[ August 19, 2007: Message edited by: Srinivasan thoyyeti ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:
If anyone has a running demo that shows that any i++ cause a race condition, I would be curios to see it.

For me, the following produces the race condition.With synchronisation I get the expected result of 200000. Without I get something between 150000 and 180000.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manfred for your posts.


counter.increment() called 10,000 times by each thread that for sure .
but why increment() missed some calls when we do not synchronize ?

I am getting 20000 without sync and with sync consistantly.
[ August 19, 2007: Message edited by: Srinivasan thoyyeti ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Manfred,

thanks for your posting.
In my case it resulted always in the output of 200000 both with synchronized or unsynchronized version.
Tried with Sun compiler (1.5.0) and Eclipse's compiler on Windows XP.


Yours,
Bu.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Burkhard,

then I would bet you have a single processor machine. Mine is a dual core processor which makes the problem much more likely to happen.

This are the dark sides with multithreading. You may test a program for several months, and as soon as it runs on another machine, it fails immediately.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred Klug ,

This is a very good lessions learnt.

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

Originally posted by Manfred Klug:
Howdy Burkhard,

then I would bet you have a single processor machine.


Yep, you won your bet.
reply
    Bookmark Topic Watch Topic
  • New Topic