Re: How to synchronize 4 threads.
- From: Eric Sosman <eric.sosman@xxxxxxx>
- Date: Fri, 29 Apr 2005 15:50:06 -0400
jseidelREMOVECAPS@xxxxxxxxxxxx wrote:
> I need to synchronize 4 different threads. I dont think wait() and
> notifyall() will work.
>
> Could someone explain how to do this?
If by "synchronize" you mean you want each thread to
run until it reaches a certain point in its execution and
then pause there until the other three have reached their
corresponding points, try something like this:
public class Barrier {
private int count;
public Barrier(int count) {
if (count <= 0)
throw new IllegalArgumentException();
this.count = count;
}
synchronized
public void stall() {
if (count <= 0)
throw new IllegalStateException();
--count;
while (count > 0) {
try {
wait();
}
catch (InterruptedException ex) {
// who cares?
}
}
notifyAll();
}
}
If you mean something else, you'll need to explain it.
--
Eric.Sosman@xxxxxxx
.
- References:
- How to synchronize 4 threads.
- From: jseidelREMOVECAPS
- How to synchronize 4 threads.
- Prev by Date: Re: How to synchronize 4 threads.
- Next by Date: Re: OutOfMemoryError - how to find root cause
- Previous by thread: Re: How to synchronize 4 threads.
- Next by thread: How to get the runtime class?
- Index(es):
Relevant Pages
|