ZetCode

Julia Channels Tutorial

last modified November 18, 2026

A channel is a first-in, first-out queue that connects producers of data with consumers. It is the structured way for Julia tasks to exchange values: put! adds an item and take! removes one. Both operations wait when the queue cannot proceed, so no busy-waiting is needed.

This tutorial covers buffering, blocking, producer/consumer patterns, and closing. Every example is sequenced with wait or close and prints from one place, so its output is deterministic. The listings are standalone main.jl files, verified with Julia 1.13.4.

A Buffered Channel

The argument of Channel{T}(n) is the buffer size: the number of items the queue can hold before a producer has to wait.

main.jl
ch = Channel{Int}(3)
put!(ch, 10)
put!(ch, 20)
println(take!(ch))
println(take!(ch))
close(ch)

Two integers are placed into a channel that can hold three, so neither call blocks. take! removes items in the order they were inserted, giving 10 and then 20. A channel is typed: Channel{Int} accepts integers.

$ julia main.jl
10
20

Buffering and Blocking

When the buffer is full, put! waits until a consumer makes room. The istaskdone predicate lets us observe that behaviour safely.

main.jl
ch = Channel{Int}(2)
put!(ch, 1)
put!(ch, 2)

t = @async begin
    put!(ch, 3)
    put!(ch, 4)
end
yield()
println(istaskdone(t))
println(take!(ch))
yield()
println(istaskdone(t))
println(take!(ch))
wait(t)
println(istaskdone(t))
println(take!(ch))
println(take!(ch))
close(ch)

After two items the buffer is full. The producer task writes 3 as soon as a slot frees but then blocks on 4, so istaskdone is false on both checks. The second take! frees enough space for the final write; the task completes and the remaining values are read in order. All output comes from the main program, which keeps the result stable.

$ julia main.jl
false
1
false
2
true
3
4

Producer and Consumer

The common pattern is one task that produces values and closes the channel when it is finished, while the consumer reads until the channel is closed.

main.jl
ch = Channel{Int}(2)

producer = @async begin
    for i in 1:5
        put!(ch, i)
    end
    close(ch)
end

println(sum(ch))
wait(producer)

The producer writes the numbers 1 through 5 and then calls close. Iterating a channel ends when it is closed and empty, so sum returns 15. The final wait confirms that the producer task has really finished. A small buffer is enough even though five items are sent: the producer simply pauses until the consumer takes some.

$ julia main.jl
15

Iterating with foreach

A channel can be used anywhere an iterable is expected, including with foreach.

main.jl
ch = Channel{String}(2)
put!(ch, "a")
put!(ch, "b")
close(ch)
foreach(println, ch)

foreach(println, ch) calls println on each value in turn and stops when the closed channel runs out of items. Closing the channel first is what tells the loop that no more values are coming.

$ julia main.jl
a
b

An Unbuffered Channel

Creating a channel without a size gives it a buffer of zero. A write then completes only when a reader is waiting: the two tasks meet in a rendezvous.

main.jl
ch = Channel{Int}()

t = @async begin
    put!(ch, 42)
end
println(take!(ch))
wait(t)
close(ch)

The background task starts a put! and waits. The main program's take! accepts the value 42, which lets the task finish. An unbuffered channel is useful when a hand-off must be synchronous, with no value sitting in a queue.

$ julia main.jl
42

The do-Block Form

Passing a function to the Channel constructor creates a channel, runs the function with it, and closes it automatically.

main.jl
result = Channel{Int}(2) do ch
    put!(ch, 3)
    put!(ch, 4)
end
println(sum(result))
println(isopen(result))

The block writes 3 and 4; the constructor then closes the channel before returning it. Summing gives 7, and isopen confirms that the channel is closed. This form removes the risk of forgetting to close a channel after a producer finishes.

$ julia main.jl
7
false

Several Producers

Channels also combine work from multiple producers. A coordinator task closes the channel once every producer is done, and the consumer collects the results.

main.jl
ch = Channel{Int}(10)

producers = [@async begin
    for j in 1:3
        put!(ch, i * 10 + j)
    end
end for i in 1:2]

coordinator = @async begin
    foreach(wait, producers)
    close(ch)
end

values = collect(ch)
wait(coordinator)
println(sort(values))

Two producers send three values each: 11, 12, 13 and 21, 22, 23. The coordinator waits for both and then closes the channel, which ends collect. Sorting the collected vector makes the printed result independent of the order in which the producers interleaved. Tasks are introduced in the tasks tutorial.

$ julia main.jl
[11, 12, 13, 21, 22, 23]

Closing a Channel

Closing a channel signals that no more values will be written. Items already in the buffer remain available.

main.jl
ch = Channel{Int}(1)
put!(ch, 7)
close(ch)
println(take!(ch))
println(isopen(ch))

The value 7 was buffered before the close and can still be taken, while isopen now returns false. Trying to take from a closed, empty channel raises an error, so consumers should use iteration or isopen to know when to stop.

$ julia main.jl
7
false

Best Practices for Channels

A few rules make channel-based programs easier to follow.

Source

Julia Parallel Computing documentation

Julia Channels and Tasks documentation

In this article we have used channels to pass values between tasks: buffered and unbuffered queues, blocking put! and take!, producer and consumer loops, the constructor's do-block form, multiple producers, and close. Tasks and cooperative scheduling are covered in Julia tasks, and arrays for holding collected results in Julia arrays.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.