# Proper use of Threads.Condition

**URL:** <https://discourse.julialang.org/t/proper-use-of-threads-condition/50937>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 29, 2020, 2:47pm UTC](https://discourse.julialang.org/t/proper-use-of-threads-condition/50937 "2020-11-29T14:47:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![felipenoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felipenoris/32/553_2.png) [@felipenoris](https://discourse.julialang.org/u/felipenoris)\
**Post date:** [November 29, 2020, 2:47pm UTC](https://discourse.julialang.org/t/proper-use-of-threads-condition/50937/1 "2020-11-29T14:47:01Z")

</div>

Hi! I’m trying to implement thread-safe event listeners, something that reminds me of [Java Event listeners](https://docs.oracle.com/javase/tutorial/uiswing/events/intro.html).

The idea is that I have many parts of a system that must react to change events in another part of the system. So a listener will wait until an event happens, handle the event and then wait for the next event.

The solution I’m giving for this problem is this code at:  
[https://gist.github.com/felipenoris/10e135bf6dcb0298057762146b4104e2](https://gist.github.com/felipenoris/10e135bf6dcb0298057762146b4104e2)

I would like feedback if I’m doing something wrong, or if there is a better way to code this.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [November 29, 2020, 2:53pm UTC](https://discourse.julialang.org/t/proper-use-of-threads-condition/50937/2 "2020-11-29T14:53:57Z")

</div>

Nice! I did something very hacky in my satellite simulator to listen to the events that come from the GUI. It will be nice to have something more polished.

---

<div class="post-metadata">

**Author:** ![rened](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rened/32/19968_2.png) [@rened](https://discourse.julialang.org/u/rened)\
**Post date:** [December 7, 2020, 10:30am UTC](https://discourse.julialang.org/t/proper-use-of-threads-condition/50937/3 "2020-12-07T10:30:48Z")

</div>

You could make use of the following idiom:

```julia
lock(condition) do
    ...
    wait(condition) # or notify(condidtion)
    ...
end

```

This uses the `try ... finally` statement internally: [Base.lock source code](https://github.com/JuliaLang/julia/blob/788b2c77c10c2160f4794a4d4b6b81a95a90940c/base/lock.jl#L148-L157)
