Listeniterationen Threadsafe gemacht

This commit is contained in:
ngb
2022-07-18 22:48:28 +02:00
parent bb50abb7bd
commit 2caa528a5e
4 changed files with 55 additions and 32 deletions

View File

@@ -1,11 +1,13 @@
package schule.ngb.zm;
import java.awt.*;
import java.awt.Graphics2D;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class DrawableLayer extends Layer {
protected LinkedList<Drawable> drawables = new LinkedList<>();
protected List<Drawable> drawables = new LinkedList<>();
protected boolean clearBeforeDraw = true;
@@ -43,7 +45,8 @@ public class DrawableLayer extends Layer {
}
synchronized( drawables ) {
for( Drawable d : drawables ) {
List<Drawable> it = Collections.unmodifiableList(drawables);
for( Drawable d : it ) {
if( d.isVisible() ) {
d.draw(drawing);
}

View File

@@ -1,7 +1,12 @@
package schule.ngb.zm;
import java.awt.Graphics2D;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@SuppressWarnings( "unused" )
public class Spielemaschine extends Zeichenmaschine {
private LinkedList<Drawable> drawables;
@@ -78,9 +83,10 @@ public class Spielemaschine extends Zeichenmaschine {
@Override
public final void update( double delta ) {
synchronized( updatables ) {
for( Updatable updatable : updatables ) {
if( updatable.isActive() ) {
updatable.update(delta);
List<Updatable> it = Collections.unmodifiableList(updatables);
for( Updatable u: it ) {
if( u.isActive() ) {
u.update(delta);
}
}
}
@@ -92,12 +98,21 @@ public class Spielemaschine extends Zeichenmaschine {
public final void draw() {
mainLayer.clear();
synchronized( drawables ) {
for( Drawable drawable : drawables ) {
if( drawable.isVisible() ) {
drawable.draw(mainLayer.getGraphics());
List<Drawable> it = Collections.unmodifiableList(drawables);
for( Drawable d: it ) {
if( d.isVisible() ) {
d.draw(mainLayer.getGraphics());
}
}
}
}
static class GraphicsLayer extends Layer {
public Graphics2D getGraphics() {
return drawing;
}
}
}

View File

@@ -1,12 +1,16 @@
package schule.ngb.zm;
import schule.ngb.zm.shapes.Shape;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Eine Leinwand ist die Hauptkomponente einer Zeichenmaschine. Sie besteht aus
@@ -195,7 +199,8 @@ public class Zeichenleinwand extends Canvas {
public void updateLayers( double delta ) {
synchronized( layers ) {
for( Layer layer : layers ) {
List<Layer> it = List.copyOf(layers);
for( Layer layer : it ) {
layer.update(delta);
}
}
@@ -232,7 +237,8 @@ public class Zeichenleinwand extends Canvas {
public void draw( Graphics graphics ) {
Graphics2D g2d = (Graphics2D) graphics.create();
synchronized( layers ) {
for( Layer layer : layers ) {
List<Layer> it = List.copyOf(layers);
for( Layer layer : it ) {
layer.draw(g2d);
}
}
@@ -256,7 +262,8 @@ public class Zeichenleinwand extends Canvas {
g2d.clearRect(0, 0, getWidth(), getHeight());
synchronized( layers ) {
for( Layer layer : layers ) {
List<Layer> it = List.copyOf(layers);
for( Layer layer : it ) {
layer.draw(g2d);
}
}

View File

@@ -6,10 +6,7 @@ import schule.ngb.zm.anim.AnimationFacade;
import schule.ngb.zm.anim.Easing;
import java.awt.Graphics2D;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.function.DoubleUnaryOperator;
public class ShapesLayer extends Layer {
@@ -45,12 +42,12 @@ public class ShapesLayer extends Layer {
return null;
}
public java.util.List<Shape> getShapes() {
public List<Shape> getShapes() {
return shapes;
}
public <ST extends Shape> java.util.List<ST> getShapes( Class<ST> shapeClass ) {
java.util.List<ST> result = new LinkedList<>();
public <ST extends Shape> List<ST> getShapes( Class<ST> shapeClass ) {
List<ST> result = new LinkedList<>();
for( Shape s : shapes ) {
if( shapeClass.isInstance(s) ) {
result.add((ST) s);
@@ -60,7 +57,7 @@ public class ShapesLayer extends Layer {
}
public void add( Shape... shapes ) {
synchronized( shapes ) {
synchronized( this.shapes ) {
for( Shape s : shapes ) {
this.shapes.add(s);
}
@@ -68,7 +65,7 @@ public class ShapesLayer extends Layer {
}
public void add( Collection<Shape> shapes ) {
synchronized( shapes ) {
synchronized( this.shapes ) {
for( Shape s : shapes ) {
this.shapes.add(s);
}
@@ -76,7 +73,7 @@ public class ShapesLayer extends Layer {
}
public void remove( Shape... shapes ) {
synchronized( shapes ) {
synchronized( this.shapes ) {
for( Shape s : shapes ) {
this.shapes.remove(s);
}
@@ -84,7 +81,7 @@ public class ShapesLayer extends Layer {
}
public void remove( Collection<Shape> shapes ) {
synchronized( shapes ) {
synchronized( this.shapes ) {
for( Shape s : shapes ) {
this.shapes.remove(s);
}
@@ -99,16 +96,16 @@ public class ShapesLayer extends Layer {
public void showAll() {
synchronized( shapes ) {
for( Shape pShape : shapes ) {
pShape.show();
for( Shape s : shapes ) {
s.show();
}
}
}
public void hideAll() {
synchronized( shapes ) {
for( Shape pShape : shapes ) {
pShape.hide();
for( Shape s : shapes ) {
s.hide();
}
}
}
@@ -147,9 +144,10 @@ public class ShapesLayer extends Layer {
}
synchronized( shapes ) {
for( Shape pShape : shapes ) {
if( pShape.isVisible() ) {
pShape.draw(drawing);
List<Shape> it = List.copyOf(shapes);
for( Shape s : it ) {
if( s.isVisible() ) {
s.draw(drawing);
}
}
}