Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Go vs Java

A basic fake stock tracking CLI application, written in both Java and Go, to compare each for simplicity, concurrency performance, memory efficiency, start-up time, etc.

stock-risk-monitor

What it does

  • Simulates live price movement for a 5-stock portfolio using geometric Brownian motion (simplified Black-Scholes noise)
  • Monitors each ticker concurrently (one goroutine/thread per ticker)
  • Fires drawdown alerts when a position drops below its configured threshold
  • Renders a live-updating terminal dashboard with P&L, market value, volatility (σ) and a risk exposure bar chart
  • Shuts down cleanly on Ctrl+C

Running it

Go

cd go
go run .

Or build a binary:

go build -o monitor .
./monitor

Java

cd java
mvn package -q
java -jar target/stock-risk-monitor-1.0-SNAPSHOT-jar-with-dependencies.jar

Docker (the real comparison)

# Go - scratch image, ~6MB
docker build -t risk-monitor-go ./go
docker run -it risk-monitor-go
# Java - JRE image, ~200MB
docker build -t risk-monitor-java ./java
docker run -it risk-monitor-java
# Compare image sizes
docker images | grep risk-monitor

Benchmarks

Run these yourself and fill in the real numbers - that's the point.

MetricGoJava
Startup timetime ./monitortime java -jar app.jar
Idle memory (RSS)ps aux | grep monitorps aux | grep java
Docker image sizedocker images risk-monitor-godocker images risk-monitor-java
Lines of codefind go -name '*.go' | xargs wc -lfind java/src -name '*.java' | xargs wc -l

Expected results (approx):

MetricGoJava
Startup~5ms~250-400ms
Idle RSS~8MB~80-150MB
Docker image~6MB~180-200MB
Lines of code~250~380

Code structure

go/
main.go - entrypoint, shutdown handling, wiring
monitor.go - concurrent engine: goroutines, channels, select
portfolio.go - data types, price simulation, default positions
ui.go - ANSI terminal renderer
java/
src/main/java/com/riskmonitor/
Main.java - entrypoint, shutdown hook
Monitor.java - ScheduledExecutorService, concurrent price engine
TickerState.java - mutable state with ReadWriteLock
Position.java - portfolio position data
Alert.java - alert value object
TickerSnapshot.java - point-in-time snapshot for UI
UI.java - ANSI terminal renderer

Key language comparisons

Concurrency

Go - goroutines are ~4KB and managed by the runtime:

gomonitor.Run(done, alertCh) // that's it

Java - requires explicit thread pool configuration:

ScheduledExecutorServicescheduler =
Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
scheduler.scheduleAtFixedRate(() -> updateTicker(...), 0, 500, TimeUnit.MILLISECONDS);

Shutdown signalling

Go - channels are first-class:

done:=make(chanstruct{})
close(done) // broadcasts to all goroutines

Java - shutdown hooks or volatile flags:

Runtime.getRuntime().addShutdownHook(newThread(() -> monitor.stop()));

Lock management

Go - defer makes unlock guaranteed and visible:

ts.mu.Lock()
deferts.mu.Unlock()
// ... do work

Java - try/finally required, more ceremony:

lock.writeLock().lock();
try {
// ... do work
} finally {
lock.writeLock().unlock();
}

Deployment

Go - single static binary, no runtime required:

FROM scratch
COPY --from=builder /app/monitor /monitor

Java - needs JRE, ~200MB minimum image:

FROM eclipse-temurin:21-jre-alpine

About

Fintech CLI app comparing Java & Go for simplicity/speed/efficiency ☕🐹

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages