Code Explainers

Code explainers tagged #concurrency

go
package metrics
 
import (
	"sync/atomic"

A lock-free request counter in Go

concurrency atomics lock-free
Intermediate 6 steps
go
package fetch
 
import (
	"context"

Concurrent API fetches with errgroup in Go

concurrency goroutines error-handling
Intermediate 8 steps
java
import java.util.concurrent.atomic.AtomicInteger;
 
public final class RequestCounter {
 

A thread-safe request counter with AtomicInteger

concurrency atomics lock-free
Intermediate 6 steps
python
import threading
import logging
 
logger = logging.getLogger(__name__)

A self-rescheduling periodic task in Python

threading scheduling concurrency
Intermediate 6 steps
go
package pipeline
 
import (
	"context"

A cancelable worker pool in Go

concurrency channels worker-pool
Advanced 8 steps
java
@Service
public class ReportGenerationService {
 
    private final ReportRepository reportRepository;

Async report generation with Spring @Async

async dependency-injection completablefuture
Intermediate 7 steps
javascript
async function mapWithConcurrency(items, limit, worker) {
  const results = new Array(items.length);
  let nextIndex = 0;
 

Bounded-concurrency async map in JavaScript

concurrency async-await promises
Intermediate 7 steps
java
public final class Debouncer<T> {
 
    private final ScheduledExecutorService scheduler =
            Executors.newSingleThreadScheduledExecutor(runnable -> {

How a debouncer collapses bursts in Java

debounce concurrency scheduling
Intermediate 9 steps
ruby
class FundsTransfer
  class InsufficientFundsError < StandardError; end
 
  def initialize(source:, destination:, amount:)

Atomic money transfers with Rails transactions

service object database transactions row locking
Advanced 9 steps
rust
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;
 

Aggregating metrics across threads in Rust

concurrency shared-state mutex
Intermediate 7 steps
java
public class ThumbnailProcessor {
 
    private static final int MAX_CONCURRENCY = 4;
 

Bounded parallel thumbnail rendering in Java

concurrency thread-pool futures
Intermediate 7 steps
rust
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration;
 

Building a thread pool in Rust

concurrency channels thread-pool
Advanced 9 steps
go
package cache
 
import (
	"container/list"

Building a generic LRU cache in Go

lru-cache generics linked-list
Intermediate 8 steps
java
@Entity
@Table(name = "accounts")
public class Account {
 

Optimistic locking with @Version in Spring

optimistic-locking jpa concurrency
Advanced 7 steps
python
import time
import threading
 
 

How a thread-safe token bucket rate limiter works

rate-limiting concurrency locking
Intermediate 6 steps
java
public class WorkPipeline {
    private final BlockingQueue<Task> queue = new LinkedBlockingQueue<>(1000);
    private static final Task POISON = new Task(-1, null);
    private final ExecutorService consumers = Executors.newFixedThreadPool(4);

A producer-consumer pipeline in Java

concurrency producer-consumer blocking-queue
Intermediate 8 steps
go
package debounce
 
import (
	"sync"

Building a debouncer in Go

concurrency debounce timers
Intermediate 6 steps
go
package retry
 
import (
	"context"

Retry with exponential backoff in Go

retry exponential-backoff jitter
Intermediate 8 steps