File size: 1,377 Bytes
b27bfb3 | 1 2 3 4 5 | {"type": "session", "harness": "opencode", "id": "lang-0039", "name": "Java Concurrency and Thread Pools"}
{"type": "message", "message": {"role": "system", "content": "You are an expert software engineer."}}
{"type": "message", "message": {"role": "user", "content": "Explain Java's concurrency model"}}
{"type": "message", "message": {"role": "assistant", "content": "Java threading: OS threads mapped 1:1 (since Java 21, virtual threads).\n\nExecutorService: thread pool abstraction.\n- Executors.newFixedThreadPool(n)\n- Executors.newCachedThreadPool()\n- ThreadPoolExecutor: core/max pool size, queue, rejection policy\n\nVirtual threads (Java 21+): lightweight threads, millions possible.\n- Thread.ofVirtual().start(runnable)\n- Pinned during synchronized blocks (use ReentrantLock instead)\n- Structured Concurrency: StructuredTaskScope\n\nCompletableFuture: async composition.\nCompletableFuture.supplyAsync(() -> fetchData())\n .thenApply(data -> transform(data))\n .thenAccept(result -> send(result))\n .exceptionally(ex -> handle(ex))\n\nSynchronized vs Lock:\n- synchronized: implicit lock, auto-release\n- ReentrantLock: explicit lock, tryLock, timeout, interruptible\n- ReadWriteLock: multiple readers, exclusive writer\n\nConcurrentHashMap: lock-striped hash map, CAS operations.\nCopyOnWriteArrayList: snapshot semantics, read-heavy workloads."}}
|