Re: How to synchronize 4 threads.





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

.



Relevant Pages

  • Re: A threading problem
    ... I dont really care which thread the send and receive happens on, ... If you want to synchronize the two methods, ... The question you posted is confusing for a couple of reasons: ... it safe to assume that you actually create the OutputBuffer timer using ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to synchronize 4 threads.
    ... > I need to synchronize 4 different threads. ... I dont think waitand ... wait and notifyall are perfectly fine. ... Prev by Date: ...
    (comp.lang.java.programmer)
  • Re: How to synchronize 4 threads.
    ... > I need to synchronize 4 different threads. ... I dont think waitand ... > notifyall() will work. ... Prev by Date: ...
    (comp.lang.java)
  • Re: Read file using multithreads
    ... to synchronize the access, thereby the UI would be blocked, and I dont know ... [MS MVP for DirectShow / MediaFoundation] ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: Synchronization
    ... We do use Active Directory. ... Although I dont know that you can change what is synchronized. ... The Synchronize is ... the server. ...
    (microsoft.public.windowsxp.general)