C
Chris Cornell
Hello all,
I have a question regarding the design pattern and implementation
details of coding a system that allows for simple and efficient
inter-thread communications from the perspective of a design pattern
approach. I'm trying to get a better understanding at designing my
java programs in a more modular fashion that can support a high level
of concurrency when needed.
Suppose I have class A that is composed of 2 different data structures
(as classes themselves) that I use frequently and need to be
synchronized (or updated with data) from an external source such as a
database, etc..
class A {
B _b;
C _c;
}
_b and _c should be updated behind the scenes on its own without
implementing any of the updating or housekeeping inside class A,
therefore _b and _c should be fully self-contained.
Sometimes however, maybe due to unforseen external events or by user
request, class A will get a request to explicitly have the data in _b
or _c updated. Since _b and _c are threads, how can I communicate
with _b and _c asynchronously to tell them to update themselves
without blocking the instruction pointer in class A.
Right now I implemented class B and class C as Runnable and in the
run() function i have it sleep(500) and check for an update flag...
etc.. well, it's not a very good implementation to say the least...
should B and C be implemented this way:
implement as a normal class (not thread or runnable) and have an
update() method in it that spawns code as a thread: ie, new Thread(
new Runnable() { ... } ); and have that set a boolean write-lock
flag on the data (or however the locking mechanism may be
implemented). So that way, I can just call update from wherever and
it will synchronize itself as needed.
There's also the possibility that _b might have to tell class _c to
update itself, etc.. or maybe some inter-class-inter-thread locking.
Any input on a good design and semantic implementation on this sort of
problem would be most appreciated.
Thanks in advance,
Chris
I have a question regarding the design pattern and implementation
details of coding a system that allows for simple and efficient
inter-thread communications from the perspective of a design pattern
approach. I'm trying to get a better understanding at designing my
java programs in a more modular fashion that can support a high level
of concurrency when needed.
Suppose I have class A that is composed of 2 different data structures
(as classes themselves) that I use frequently and need to be
synchronized (or updated with data) from an external source such as a
database, etc..
class A {
B _b;
C _c;
}
_b and _c should be updated behind the scenes on its own without
implementing any of the updating or housekeeping inside class A,
therefore _b and _c should be fully self-contained.
Sometimes however, maybe due to unforseen external events or by user
request, class A will get a request to explicitly have the data in _b
or _c updated. Since _b and _c are threads, how can I communicate
with _b and _c asynchronously to tell them to update themselves
without blocking the instruction pointer in class A.
Right now I implemented class B and class C as Runnable and in the
run() function i have it sleep(500) and check for an update flag...
etc.. well, it's not a very good implementation to say the least...
should B and C be implemented this way:
implement as a normal class (not thread or runnable) and have an
update() method in it that spawns code as a thread: ie, new Thread(
new Runnable() { ... } ); and have that set a boolean write-lock
flag on the data (or however the locking mechanism may be
implemented). So that way, I can just call update from wherever and
it will synchronize itself as needed.
There's also the possibility that _b might have to tell class _c to
update itself, etc.. or maybe some inter-class-inter-thread locking.
Any input on a good design and semantic implementation on this sort of
problem would be most appreciated.
Thanks in advance,
Chris