tinycodinglm / lang /lang-0039.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.38 kB
opencode trace
System You are an expert software engineer.

You are an expert software engineer.

User

Explain Java's concurrency model

Assistant

Java threading: OS threads mapped 1:1 (since Java 21, virtual threads).

ExecutorService: thread pool abstraction.

  • Executors.newFixedThreadPool(n)
  • Executors.newCachedThreadPool()
  • ThreadPoolExecutor: core/max pool size, queue, rejection policy

Virtual threads (Java 21+): lightweight threads, millions possible.

  • Thread.ofVirtual().start(runnable)
  • Pinned during synchronized blocks (use ReentrantLock instead)
  • Structured Concurrency: StructuredTaskScope

CompletableFuture: async composition.
CompletableFuture.supplyAsync(() -> fetchData())
.thenApply(data -> transform(data))
.thenAccept(result -> send(result))
.exceptionally(ex -> handle(ex))

Synchronized vs Lock:

  • synchronized: implicit lock, auto-release
  • ReentrantLock: explicit lock, tryLock, timeout, interruptible
  • ReadWriteLock: multiple readers, exclusive writer

ConcurrentHashMap: lock-striped hash map, CAS operations.
CopyOnWriteArrayList: snapshot semantics, read-heavy workloads.