Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə17/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   ...   13   14   15   16   17   18   19   20   ...   47
Java Interview Guide

The Volatile Keyword
The volatile keyword is used to indicate that the value of a variable may
be modified by multiple threads. Java’s memory model permits threads to cache
the value of variables for performance. The volatile keyword must be used
if a method relies on the latest value of a shared variable. For example, a method
that executes a while(!finished) loop would need to declare the
finished
variable as volatile if a separate thread was responsible for
updating its value.
The guarantee that a memory write will be visible to another block of code
is called a happens-before relationship. Both the volatile keyword and the
synchronize
keyword guarantee a happens-before relationship, however the
synchronize
keyword also guarantees atomic access to a block of code.


Concurrent Data Structures
Java 5 introduced several high-level data structures in the
java.util.concurrent
package. CopyOnWriteArrayList
is a thread-safe alternative to ArrayList that copies the underlying array
during modifications. ConcurrentHashMap is a thread-safe alternative
to HashMap that only incurs a synchronization penalty on segments of the map
that were modified. The BlockingQueue was introduced to allow producers
to block and wait while a queue is full, and allow consumers to block and wait
while a queue is empty.


The Executor Framework
The executor framework provides a layer of abstraction over multithreaded task
execution. The ExecutorService manages a thread pool that accepts
Runnable
or Callable tasks. Submitting a task immediately returns
a Future object, which contains methods that return the status and result
of a running task. The executor framework effectively decouples tasks from their
execution policies.



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   ...   13   14   15   16   17   18   19   20   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin