Zum Inhalt springen

Nebenläufigkeiten/Threads


Empfohlene Beiträge

Geschrieben

Hi,

kann mir jemand erklären wie es hier zu einem Deadlockproblem kommen kann, also ohne das ich ich die Methoden (put,get)Synchronisiert habe?

public class BoundedQueue {

private List data = new ArrayList();

public boolean isEmpty() { return data.isEmpty(); }

public void put(Object x) { data.add(x); }

public Object get() { return data.remove(0); }

}

Merci

Geschrieben

Schau doch mal in die API Doc der Klasse ArrayList, dort steht folgendes (Version 1.4.2, das war der erste Google Treffer, privat habe ich die API Doc nicht gebookmarked):

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:

List list = Collections.synchronizedList(new ArrayList(...));

Peter

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...