Java (Android), thread safe FIFO without locking? -
i have producer-consumer situation 2 threads. 1 takes objects pool , puts them in fifo, other 1 reads objects (multiples @ time), calculations, removes them list , puts them in pool. concurrentlinkedqueue pattern should thread safe without additional locks. each object written once, read 1 time , removed once. add() , poll() safe in clq. a) correct? b) other containers back upwards specific pattern? remember things linkedlist or arraylist beingness safe because of atomic effects "getsize()" or "head=..." not sure , not find it.
yes, methods add
, poll
of concurrentlinkedqueue
thread-safe (as other methods). no, not utilize arraylist
or linkedlist
straight in concurrent environment. these classes not thread-safe definition:
note implementation not synchronized. if multiple threads access arraylist instance concurrently, , @ to the lowest degree 1 of threads modifies list structurally, must synchronized externally.
if not satisfied concurrentlinkedqueue
, have @ container implementations in bundle java.util.concurrent
:
concurrentlinkeddeque
(is queue
) linkedblockingqueue
(is blockingqueue
) linkedblockingdeque
(is blockingdeque
) arrayblockingqueue
(is blockingqueue
) i assume, either queue
or blockingqueue
interface of choice.
java android multithreading list
No comments:
Post a Comment