FlashKeepers

Java · College · College and University

Java V: Concurrency

Threads, Runnable, synchronized, volatile, ExecutorService, CompletableFuture, locks, atomics, concurrent collections and virtual threads.

About this set

40 mixed cards for College Java. Set 5 of 6 in Java. It opens with "Why is implementing Runnable preferred to extending Thread?" and "What does Thread.start() do?". Study it alongside the other College sets below.

40 cards · mixed cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

Why is implementing Runnable preferred to extending Thread?
Show answerJava doesn't support multiple inheritance, so extending Thread locks you into that hierarchy. Runnable is an interface, allowing classes to extend another class too.
What does Thread.start() do?
Show answerStarts a new thread and calls run() inside that thread. Does not execute run() in the current thread.
What happens if you call Thread.run() directly?
Show answerThe run() method executes in the current thread, not a new thread. No concurrent execution occurs.
How does the synchronized keyword protect code?
Show answerIt uses the object's monitor (intrinsic lock) to ensure only one thread can execute the synchronized block or method at a time.
Can a synchronized method call another synchronized method on the same object without deadlock?
Show answerYes, because ReentrantLock behavior is built into Java monitors. The same thread can reacquire the lock.
What does the volatile keyword guarantee?
Show answerIt ensures that changes to a volatile variable are immediately visible to all threads. It does not provide atomicity.
How does volatile differ from synchronized in terms of performance?
Show answerVolatile is lighter-weight than synchronized. It only ensures visibility, not mutual exclusion, so multiple threads can access a volatile variable simultaneously.
What does ExecutorService abstract?
Show answerIt abstracts thread creation and management, allowing you to submit tasks without manually creating and managing Thread objects.
What does ExecutorService.newFixedThreadPool(10) create?
Show answerA thread pool with 10 threads that reuse themselves for submitted tasks. The pool size is fixed.
How do execute() and submit() differ in ExecutorService?
Show answerexecute(Runnable) returns void and throws checked exceptions. submit() returns a Future and wraps checked exceptions in unchecked exceptions.
What is the difference between shutdown() and shutdownNow() on ExecutorService?
Show answershutdown() stops accepting new tasks but waits for existing tasks to complete. shutdownNow() stops accepting new tasks and attempts to interrupt running tasks.
What does CompletableFuture.thenApply() do?
Show answerIt takes a Function that transforms the result of the previous stage and returns a new CompletableFuture with the transformed result.
What does CompletableFuture.thenAccept() take as a parameter?
Show answerA Consumer that accepts the result but returns void. It does not transform the value.
How does CompletableFuture.join() differ from get()?
Show answerjoin() returns the result or throws an unchecked CompletionException. get() throws checked ExecutionException and InterruptedException.
What type of lambda does CompletableFuture.supplyAsync() accept?
Show answerA Supplier with no parameters that returns a value. It is used to asynchronously compute and provide a result.

25 more cards in the app

Study this set free