Machine-translated by Google. Questions are authored in English.
Top

Threads - General Questions Questions and Answers

Answer: C

Correct answer: ThreadLocalRandom.current().nextInt(10)

ThreadLocalRandom gives each thread its own generator, which removes the contention that a shared Random suffers under concurrency. The instance is reached through the static current(); the class cannot be instantiated directly, and the generating methods are not themselves static.

Please log in or register to report this question.

Answer: D

Correct answer: CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> load());

supplyAsync takes a Supplier and returns a future of its result, which is what is wanted when the work produces a value. runAsync takes a Runnable and gives CompletableFuture<Void>, so it cannot carry the loaded string.

Please log in or register to report this question.

93.

Which of these is the correct way to count the threads in a ThreadGroup into an array?

Answer: A

enumerate copies references into a caller-supplied array and returns how many it wrote. The result is an estimate, because threads may start or finish during the copy, which is why the API does not simply return a fresh array.

Please log in or register to report this question.

Answer: B

Thread has a constructor taking a name, so a subclass passes it up with super. The name field is private, a superclass constructor cannot be called like a method, and setName takes an argument. setName may also be used after construction.

Please log in or register to report this question.

95.

Which of these declarations makes a field visible to other threads without making compound updates atomic?

Answer: C

volatile guarantees that a write is seen by later reads on other threads, which is exactly what a plain or static field does not promise. It says nothing about a read-modify-write pair, and the keyword cannot be applied to a field in place of synchronized, which is not legal there at all.

Please log in or register to report this question.

Answer: D

The milliseconds argument is a long and the nanoseconds a small int, and the method is final and void. Returning a boolean would have let a caller distinguish a timeout from a notification, which this API deliberately does not offer.

Please log in or register to report this question.

97.

Which of these types is generic?

Answer: A

ThreadLocal is declared with a type parameter, so its get and set exchange that type without a cast. Thread, ThreadGroup and InterruptedException carry no type parameter, which is why a Thread cannot be declared to produce a particular result type.

Please log in or register to report this question.

Answer: B

The constructor takes the initial party count, and unlike CyclicBarrier that count can change afterwards with register and arriveAndDeregister. The no-argument form exists but starts with none registered, and there is no static factory.

Please log in or register to report this question.

Answer: C

Correct answer: long stamp = sl.tryOptimisticRead();

An optimistic read returns a stamp rather than a lock, and no lock is actually taken - the reader does its work and then calls validate(stamp) to find out whether a writer intervened. A zero stamp means the attempt failed, and a failed validation means the work must be redone under a real read lock.

Please log in or register to report this question.

Loading…
Tags: Threads - General Questions Questions and Answers || Threads - General Questions MCQ Questions and Answers || Threads - General Questions GK Questions and Answers || Threads - General Questions GK MCQ Questions || Threads - General Questions Multiple Choice Questions and Answers || Threads - General Questions GK || GK on Threads - General Questions || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming