Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jul 2012
    Messaggi
    21

    ArrayList e concorrenza

    Gli arrayList in java possono gestire la concorrenza? nel senso, se io in 2 thread diversi, in uno inserisco un elemento e nell'altro ne tolgo uno, posso avere problemi (magari size sbagliata o altro)?

  2. #2
    Utente di HTML.it L'avatar di neroux
    Registrato dal
    Aug 2009
    Messaggi
    1,973
    http://docs.oracle.com/javase/7/docs...ArrayList.html
    Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

    www.sitemeer.com » Quando un sito pare irraggiungibile

    Se ti piace ci puoi trovare anche su Facebook

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    755
    Dalla documentazione ufficiale:

    Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally...bla bla bla
    ArrayList non è thread-safe ma ci sono vari modi per renderla tale..cerca su google e troverai molto materiale oppure lavora un po' con la fantasia

    ArrayList API



    EDIT: mi hanno anticipato

  4. #4
    Utente di HTML.it
    Registrato dal
    Jul 2012
    Messaggi
    21
    E c'è già qualche costrutto thread-safe?

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    755
    Non vorrei dire cavolate ma penso che i Vector lo siano..

    Oppure puoi ricorrere "all'artifizio"
    codice:
    Collections.synchronizedList(List list)

  6. #6
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    613
    Originariamente inviato da zipangulu
    Non vorrei dire cavolate ma penso che i Vector lo siano..

    Oppure puoi ricorrere "all'artifizio"
    codice:
    Collections.synchronizedList(List list)
    Esatto, è la differenza principale fra Vector ed ArrayList.
    effeffe

  7. #7
    Quello che ti serve è la LinkedBlockingQueue
    ...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.