Welcome to the JavaGo Library
JavaGo is a library that implements Go's concurrency features in Java, so you can write multithreaded applications in Java as easily as in Go.
Overview
Go's philosophy when it comes to concurrency is that programmers should not communicate by sharing memory,
instead they should share memory by communicating. This library brings this philosophy to Java by
implementing the go
and select
statements along with channels, wait groups, and
the entire sync package.
This means that we can translate the following Go code on the left to the following Java code on the right.
// Sample Go program that starts a goroutine which counts to 10.
package main
import (
"fmt"
"sync"
)
func count(n int, wg *sync.WaitGroup) {
defer wg.Done()
for i := 1; i <= n; i++ {
fmt.Println(i)
}
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
go count(10, &wg)
wg.Wait()
}
import io.javago.sync.WaitGroup;
import static io.javago.Go.go;
public class Count {
public static void main(String[] args) {
WaitGroup wg = new WaitGroup();
wg.add(1);
go(() -> count(10, wg));
wg.await();
}
private static void count(int n, WaitGroup wg) {
try (wg) {
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}
}
We are now able to write a multithreaded Java program without touching the Thread
class or
java.util.concurrent
package at all. This makes multithreaded programming in Java much simpler
and easier.
Install
To use this library, please add the following lines to the dependencies
section
of your pom.xml file.
<dependency>
<groupId>io.javago</groupId>
<artifactId>javago</artifactId>
<version>1.0.0</version>
</dependency>
Documentation
Detailed documentation for this package can be found here.
To view the source code for this library, click here.
This library requires Java 21 as it makes use of virtual threads.
Blog
I have published a series of articles on my Medium blog documenting how I created this library. A link to each part can be found below.
- Implementing the Go Keyword and Wait Groups
- Implementing Channels
- Implementing the Select Statement
- Implementing the Sync Package
To stay up to date with anything else I create, I encourage you to follow my Medium blog.
About
My name is William Yin, and I wrote this library because I was fascinated by how fun and painless Go made multithreaded programming. I hope that by using this library, you will find multithreaded programming in Java just as fun and painless as in Go.