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

 
 
castillo.bryan@gmail.com
Guest
Posts: n/a
 
      12-19-2006

John Ersatznom 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));



Why not just use AtomicBoolean then?

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


On Dec 17, 7:44 pm, John Ersatznom <(E-Mail Removed)> 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));


Its rare that you want to new a Boolean, its better to use the already
existing constant


Container<Boolean> booleanHolder = new Container<Boolean>(
Boolean.FALSE);

 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      12-19-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> John Ersatznom wrote:
>> John Ersatznom wrote:


>>> public class Container<Foo> {


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

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


There are good reasons for making type variables look different from types.

> Why not just use AtomicBoolean then?


Or even just a volatile boolean.

Adding the keyword 'synchronized' does not automatically make the code
thread-safe.

Tom Hawtin
 
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