• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boss/worker examples

 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to use the producer/consumer pattern as boss/worker where there can only be ONE producer and MANY consumers. I am looking for some sample code.
In addition, is it possible to find out from an object how many instances there are of its class?
Thanks,
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is it possible to find out from an object how many instances there are of its class?

Use a static counter. In your class's constructor(s) increment this counter. Decrement it in finalize(). Of course, make sure this counter is thread-safe.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anthony Villanueva:

Use a static counter. ...
Decrement it in finalize().

So I guess there is no standard way of doing so.
BTW, just to confirm finalize() does not need to be synchronized, or does it?
I would guess not, I imagine that some "single" low priority thread cleans up all the objects.
Finally, how about some example code please?
Thanks Anthony and the rest,
Leslie
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes finalize will require synchronization. That is going to put some slowless on your object collection but if your not speed concerned, then your cool.
 
satyendra gurjar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leslie Chaim:

So I guess there is no standard way of doing so.
BTW, just to confirm finalize() does not need to be synchronized, or does it?
I would guess not, I imagine that some "single" low priority thread cleans up all the objects.
Finally, how about some example code please?
Thanks Anthony and the rest,
Leslie

Here is the code for to count the number of objects created of a class.
We need to protect out static counter from simultaneous access and thats why I made increment and decrement methods synchronized.

This may be right that only one thread can access finalize at any time so we dont have to synchronized it, but still our static counter can be accessed by multiple threads at the same time since its not attached with the object.

//Satyendra
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be right that only one thread can access finalize at any time so we dont have to synchronized it, but still our static counter can be accessed by multiple threads at the same time since its not attached with the object.
Yup. Note that even if you don't intentionally create any threads beside the main thread, synchronization is necessary because garbage collection and finalization are generally handled by a seperate thread (not main).
The getCounter() method really needs to be synchronized too, particularly since it's getting a long value. Otherwise you could easily get complete garbage when you read that value.
 
satyendra gurjar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getCounter() method really needs to be synchronized too, particularly since it's getting a long value. Otherwise you could easily get complete garbage when you read that value.

I'm confused pl help me to understand this.
1. "counter" is a static member of class X, once its initialized with the ObjectCounter 's object reference, "counter" should refer to a live object for the whole application life time ?? That means since static member is not attached to an object once they created they have to remain alive for the application life time ?? So "counter" can be garbage collected only at application termination ??
2. what do you mean by "particularly since it's getting a long value", I suppose since I'm reading the value from an object which has application life time I dont have to synchronize it ?? And whats that special case about "long".
Thanks in advance.
//satyendra
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satyendra:
This may be right that only one thread can access finalize at any time so we dont have to synchronized it, but still our static counter can be accessed by multiple threads at the same time since its not attached with the object.

Wrong. In 1.4 their is concurrent garbage collection mode. In this case, objects can be created and destroyed at the same time. You definitely need synchronization. In any case, you never know how any JVM will behave with respect to instantiation and garbage collection. Be safe.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satyendra gurjar:

1. "counter" is a static member of class X, once its initialized with the ObjectCounter 's object reference, "counter" should refer to a live object for the whole application life time ?? That means since static member is not attached to an object once they created they have to remain alive for the application life time ?? So "counter" can be garbage collected only at application termination ??

True, but null pointers are not the ony reason for synchronization.
Originally posted by satyendra gurjar:

2. what do you mean by "particularly since it's getting a long value", I suppose since I'm reading the value from an object which has application life time I dont have to synchronize it ?? And whats that special case about "long".

The operations are not atomic. Thats the problem. long values take 2 operations to be read, and if a write occurs between the 2 reads, you will get a garbage read value. ++x is also not atomic, so it must be synchronized, else you can easily miss one increment.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!