Re: "event"-like thread synchronization object?

Discussion in 'Java' started by Arne Vajhøj, Jul 17, 2008.

  1. Peter Duniho wrote:
    > Okay, my curiosity has the best of me, but I can't find the specific
    > type I'm thinking of.
    > In some APIs, there's an object that maintains a simple "signaled"
    > state, and which a thread can wait on. For example, in Windows, there's
    > an event handle, which can either reset automatically when a thread is
    > released based on it, or optionally can remain in whatever state until
    > it's explicitly changed by the code.
    > I have seen several different classes in Java that handle thread
    > synchronization, but none appear to have this exact simple behavior.
    > The closest I see is simply using the wait() and notify() methods on an
    > object, but those require that the object's monitor be acquired by the
    > caller, and also has this "spurious wakeup" issue that the documentation
    > describes (which I admit, I don't fully understand _why_ it can happen).
    > Neither issue is a really big deal, but it does make for somewhat more
    > convoluted code (because the both the waiting and setting need to wind
    > up in a "synchronized" block, and the waiting needs to include a
    > separate signalling flag and a loop to check the flag). It works, but
    > it's a bit messy.
    > An alternative that I used successfully was the CountDownLatch class; it
    > addresses the two issues that wait()/notify() have, but is a one-shot
    > thing. It can't be used repeatedly. The docs for CountDownLatch
    > suggest using the CyclicBarrier as a re-usable alternatve to
    > CountDownLatch, but it's really not the same thing. In particular, it
    > doesn't allow one thread to simply set the "okay to proceed" state and
    > continue; all the threads need to wait before any can proceed.
    > The fact is, even from what I've seen, Java's got enough threading
    > support to effectively provide the behavior I'm looking for. But
    > nothing I've found so far does so in quite the same way as the Windows
    > event handle object. I'm curious if those who are more familiar with
    > threading in Java know of such a class and/or can suggest better
    > alternatives than the ones I've found already.

    java.util.concurrent.Semaphore ?

    Arne Vajhøj, Jul 17, 2008
    1. Advertising

  2. Arne Vajhøj

    Mark Space Guest

    Arne Vajhøj wrote:

    > java.util.concurrent.Semaphore ?
    > Arne

    I was thinking this over today and came to the same conclusion. A
    binary Semaphore is a pretty good replacement for auto mode, where one
    thread is released per signal.

    AtomicInteger might work well for some problems too.

    Actually, the more I think about it, the more I think a "manual" Windows
    EventHandle is just some sort of low level generic form of IPC intended
    for "roll your own" implementations. It might just be an volatile
    boolean or int. Both of those are "manual mode IPC" in Java, intended
    for rolling your own IPC methods.
    Mark Space, Jul 17, 2008
    1. Advertising

  3. In article <>,
    "Peter Duniho" <> wrote:

    > both the waiting and setting need to wind up in a "synchronized" block

    > An important aspect of the event handle in Windows is that the
    > thread scheduler understands it and uses it to ascertain
    > runnability.


    I don't know if it's apropos, but something about this reminds me of the
    notion of a rendezvous with an entry/barrier:


    And some literature from a nearby sect: :)


    John B. Matthews
    trashgod at gmail dot com
    home dot woh dot rr dot com slash jbmatthews
    John B. Matthews, Jul 17, 2008
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vincent Lascaux

    Thread synchronization problem

    Vincent Lascaux, Aug 31, 2004, in forum: Java
    John C. Bollinger
    Sep 2, 2004
  2. Razvan

    Thread synchronization

    Razvan, Aug 31, 2004, in forum: Java
    Lee Fesperman
    Sep 26, 2004
  3. Adam Warner
    Patricia Shanahan
    Mar 28, 2006
  4. Patrick Kowalzick
    Patrick Kowalzick
    Mar 14, 2006
  5. Mark Space
    Mark Space
    Jul 16, 2008

Share This Page