Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to use wait() and notifyAll() in simple container object

Reply
Thread Tools

How to use wait() and notifyAll() in simple container object

 
 
Bryan
Guest
Posts: n/a
 
      12-15-2006
Hello all,

I have a simple container object that has a get and set method for a
variable it contains. Multiple threads will potentially be accessing
the container object to get and set the variable. Can anyone tell me
what's wrong with the code I have below? It's not working for me...

public class Container {

private boolean value = false;
private boolean available = false;

public Container(boolean value) {
this.value = value;
}

public synchronized boolean get() {
while (available == false) {
try {
wait();
} catch (InterruptedException ex) { ex.printStackTrace(); }
}
available = false;
notifyAll();
return value;
}

public synchronized void set(boolean value) {
while (available == true) {
try {
wait();
} catch (InterruptedException ex) { ex.printStackTrace(); }
}
this.value = value;
available = true;
notifyAll();
}
}

I found most of the above code in a tutorial on the web --
http://www.janeg.ca/scjp/threads/synchronized.html -- but like I said
it's not working for me. Here's how I'm testing it:

I have a class that extends TimerTask and is called every 5 seconds to
randomize the value by calling the set method and passing it a random
boolean value obtained from Random.nextBoolean(). I have it set up to
print to the console each time it changes the value... it prints one
time but never prints again. Any suggestions as to why this isn't
working for me?

Thanks!

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      12-15-2006
Bryan wrote:
> Hello all,
>
> I have a simple container object that has a get and set method for a
> variable it contains. Multiple threads will potentially be accessing
> the container object to get and set the variable. Can anyone tell me
> what's wrong with the code I have below? It's not working for me...
>
> public class Container {
>
> private boolean value = false;
> private boolean available = false;
>
> public Container(boolean value) {
> this.value = value;
> }
>
> public synchronized boolean get() {
> while (available == false) {
> try {
> wait();
> } catch (InterruptedException ex) { ex.printStackTrace(); }
> }
> available = false;
> notifyAll();
> return value;
> }
>
> public synchronized void set(boolean value) {
> while (available == true) {
> try {
> wait();
> } catch (InterruptedException ex) { ex.printStackTrace(); }
> }
> this.value = value;
> available = true;
> notifyAll();
> }
> }
>
> I found most of the above code in a tutorial on the web --
> http://www.janeg.ca/scjp/threads/synchronized.html -- but like I said
> it's not working for me. Here's how I'm testing it:
>
> I have a class that extends TimerTask and is called every 5 seconds to
> randomize the value by calling the set method and passing it a random
> boolean value obtained from Random.nextBoolean(). I have it set up to
> print to the console each time it changes the value... it prints one
> time but never prints again. Any suggestions as to why this isn't
> working for me?
>
> Thanks!


> it's not working for me

'It' is your code, not the tutorial code which is too simple to have
any problem.
Post a small demo code that is generally compilable, runnable and could
reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      12-16-2006
Bryan wrote:

> I have a class that extends TimerTask and is called every 5 seconds to

You should use multiple threads accessing your single Container object.
Otherwise, you can't test the synchronized/wait/notify mechanism.

'Container' is a bad naming.

 
Reply With Quote
 
kilian heckrodt
Guest
Posts: n/a
 
      12-16-2006
Bryan wrote:
> Hello all,
>
> I have a simple container object that has a get and set method for a
> variable it contains. Multiple threads will potentially be accessing
> the container object to get and set the variable. Can anyone tell me
> what's wrong with the code I have below? It's not working for me...
>
> public class Container {
>
> private boolean value = false;
> private boolean available = false;
>
> public Container(boolean value) {
> this.value = value;
> }
>
> public synchronized boolean get() {
> while (available == false) {
> try {
> wait();
> } catch (InterruptedException ex) { ex.printStackTrace(); }
> }
> available = false;
> notifyAll();
> return value;
> }
>
> public synchronized void set(boolean value) {
> while (available == true) {
> try {
> wait();
> } catch (InterruptedException ex) { ex.printStackTrace(); }
> }
> this.value = value;
> available = true;
> notifyAll();
> }
> }
>
> I found most of the above code in a tutorial on the web --
> http://www.janeg.ca/scjp/threads/synchronized.html -- but like I said
> it's not working for me. Here's how I'm testing it:
>
> I have a class that extends TimerTask and is called every 5 seconds to
> randomize the value by calling the set method and passing it a random
> boolean value obtained from Random.nextBoolean(). I have it set up to
> print to the console each time it changes the value... it prints one
> time but never prints again. Any suggestions as to why this isn't
> working for me?
>
> Thanks!
>

Hmmm... the example at the website looks ok, but i wonder whether you
misunderstood its purpose, i.e. how the consumer-producer scenario works.

The idea is that you _cannot_ set ("produce") a new value _before_ the
old is consumed. That means to call set for a 2nd time and having an
effect, you need to call get() before, which consumes the value and lets
you set/produce another later.
The consumer-producer scenario is to avoid race conditions and to make
sure no information gets overwritten by accident (a value that is
replaced by another via a set() call before it was consumed by a get() call.
This is a typical scenario for a multithreaded buffer, where you want to
avoid that its content gets overwritten by incoming new data before it
was read, since that would mean data loss









 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      12-17-2006
Ah yes... I did in fact misunderstand the purpose. Thanks for clearing
that up. I simply want to make sure that one thread doesn't try to
read at the same time another thread is writing. If so, I want the
read to come after the write. Otherwise, I'm not worried about data
loss.

Thanks!

> Hmmm... the example at the website looks ok, but i wonder whether you
> misunderstood its purpose, i.e. how the consumer-producer scenario works.
>
> The idea is that you _cannot_ set ("produce") a new value _before_ the
> old is consumed. That means to call set for a 2nd time and having an
> effect, you need to call get() before, which consumes the value and lets
> you set/produce another later.
> The consumer-producer scenario is to avoid race conditions and to make
> sure no information gets overwritten by accident (a value that is
> replaced by another via a set() call before it was consumed by a get() call.
> This is a typical scenario for a multithreaded buffer, where you want to
> avoid that its content gets overwritten by incoming new data before it
> was read, since that would mean data loss


 
Reply With Quote
 
andrewmcdonagh
Guest
Posts: n/a
 
      12-17-2006


On Dec 17, 4:51 pm, "Bryan" <BTRichard...@gmail.com> wrote:
> Ah yes... I did in fact misunderstand the purpose. Thanks for clearing
> that up. I simply want to make sure that one thread doesn't try to
> read at the same time another thread is writing. If so, I want the
> read to come after the write. Otherwise, I'm not worried about data
> loss.
>
> Thanks!
>
> > Hmmm... the example at the website looks ok, but i wonder whether you
> > misunderstood its purpose, i.e. how the consumer-producer scenario works.

>
> > The idea is that you _cannot_ set ("produce") a new value _before_ the
> > old is consumed. That means to call set for a 2nd time and having an
> > effect, you need to call get() before, which consumes the value and lets
> > you set/produce another later.
> > The consumer-producer scenario is to avoid race conditions and to make
> > sure no information gets overwritten by accident (a value that is
> > replaced by another via a set() call before it was consumed by a get() call.
> > This is a typical scenario for a multithreaded buffer, where you want to
> > avoid that its content gets overwritten by incoming new data before it
> > was read, since that would mean data loss



If thats all you want to do,then you have a lot less choices...

1) Don't use objects whose values can be changed - use multiple
'immutable' objects. So the first thread uses the first object, then
the second thread one uses a different object based upon the value that
correct for it. (sounds compilcated and it is in some ways, but you
get great performance.

2) - The one which sounds like the best for your case. - Just
synchronize your methods like you have, without all of the extra
gubbings.

e.g.

public class Container {

private boolean value = false;

public Container(boolean value) {
this.value = value;
}

public synchronized boolean get() {
return value;
}

public synchronized void set(boolean value) {
this.value = value;
}

}

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-17-2006
> public class Container {
>
> private boolean value = false; // redundant assignment
>
> public Container(boolean value) {
> this.value = value;
> }
>
> public synchronized boolean get() {
> return value;
> }
>
> public synchronized void set(boolean value) {
> this.value = value;
> }
>
> }


Looks like a mutable, synchronized Boolean. Might want to consider a class
name like SynchronizedBoolean. Might want to rename the get()/set() methods,
either to follow JavaBean conventions or to mimic similar methods in
java.lang.Boolean.

- Lew
 
Reply With Quote
 
John Ersatznom
Guest
Posts: n/a
 
      12-17-2006
Lew wrote:
>> public class Container {
>>
>> private boolean value = false; // redundant assignment
>>
>> public Container(boolean value) {
>> this.value = value;
>> }
>>
>> public synchronized boolean get() {
>> return value;
>> }
>>
>> public synchronized void set(boolean value) {
>> this.value = value;
>> }
>>
>> }

>
>
> Looks like a mutable, synchronized Boolean. Might want to consider a
> class name like SynchronizedBoolean. Might want to rename the
> get()/set() methods, either to follow JavaBean conventions or to mimic
> similar methods in java.lang.Boolean.
>
> - Lew


Why not

public class Container<Foo> {
private Foo contents;
public Container (Foo initialContents) {
contents = initialContents;
}
public synchronized Foo getContents () {
return contents;
}
public synchronized setContents (Foo newContents) {
contents = newContents;
}
}

Container<Foo> fooHolder = new Container<Foo>(new Boolean(false));

It's reusable for holding any object now.
 
Reply With Quote
 
John Ersatznom
Guest
Posts: n/a
 
      12-17-2006
John Ersatznom wrote:
> Lew wrote:
>
>>> public class Container {
>>>
>>> private boolean value = false; // redundant assignment
>>>
>>> public Container(boolean value) {
>>> this.value = value;
>>> }
>>>
>>> public synchronized boolean get() {
>>> return value;
>>> }
>>>
>>> public synchronized void set(boolean value) {
>>> this.value = value;
>>> }
>>>
>>> }

>>
>>
>>
>> Looks like a mutable, synchronized Boolean. Might want to consider a
>> class name like SynchronizedBoolean. Might want to rename the
>> get()/set() methods, either to follow JavaBean conventions or to mimic
>> similar methods in java.lang.Boolean.
>>
>> - Lew

>
>
> Why not
>
> public class Container<Foo> {
> private Foo contents;
> public Container (Foo initialContents) {
> contents = initialContents;
> }
> public synchronized Foo getContents () {
> return contents;
> }
> public synchronized setContents (Foo newContents) {
> contents = newContents;
> }
> }
>
> Container<Foo> fooHolder = new Container<Foo>(new Boolean(false));


Meh, make that last

Container<Boolean> booleanHolder = new Container<Boolean>(new
Boolean(false));
 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      12-19-2006
Thanks for the comments guys. Looks like I'll go with synchronizing
the get/set methods!

On Dec 17, 12:44 pm, John Ersatznom <j.ers...@nowhere.invalid> wrote:
> John Ersatznom wrote:
> > Lew wrote:

>
> >>> public class Container {

>
> >>> private boolean value = false; // redundant assignment

>
> >>> public Container(boolean value) {
> >>> this.value = value;
> >>> }

>
> >>> public synchronized boolean get() {
> >>> return value;
> >>> }

>
> >>> public synchronized void set(boolean value) {
> >>> this.value = value;
> >>> }

>
> >>> }

>
> >> Looks like a mutable, synchronized Boolean. Might want to consider a
> >> class name like SynchronizedBoolean. Might want to rename the
> >> get()/set() methods, either to follow JavaBean conventions or to mimic
> >> similar methods in java.lang.Boolean.

>
> >> - Lew

>
> > Why not

>
> > public class Container<Foo> {
> > private Foo contents;
> > public Container (Foo initialContents) {
> > contents = initialContents;
> > }
> > public synchronized Foo getContents () {
> > return contents;
> > }
> > public synchronized setContents (Foo newContents) {
> > contents = newContents;
> > }
> > }

>
> > Container<Foo> fooHolder = new Container<Foo>(new Boolean(false));Meh, make that last

>
> Container<Boolean> booleanHolder = new Container<Boolean>(new
> Boolean(false));


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57