lichtsprung/CGASceneGraphIssues — Issue #9: Interne Synchronisierung fehlerhaft
Author: stetro
Created: 2011-11-18 10:47:29+00:00
State: open

Die interne ArrayList wird nicht Synchron behandet. Folgendes Beispiel wirft unvorhersehbar Exceptions nach Tastatureingaben:

```java
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import cga.Framework;
import cga.framework.renderer.RendererJ2d;
import cga.framework.shape.Line2d;

public class Test2Main extends Framework {

public Test2Main(int width, int height) {
super(width, height);
((RendererJ2d) this.renderer).addKeyListener(new MyKeyListener());
}

@Override
public void initGraph() {}

public static void main(String args[]) {
Test2Main tm = new Test2Main(300, 300);
tm.start();
}

private class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
add(new Line2d(0, 0, 5, 5));
}
}
}
```

Kapsele ich mein Shape welches eine Eigene ArrayList beinhaltet wird via synchronized diese Exception nicht geworfen:

```java
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import cga.Framework;
import cga.framework.camera.Camera;
import cga.framework.math.Matrix;
import cga.framework.renderer.Renderer;
import cga.framework.renderer.RendererJ2d;
import cga.framework.shape.Line2d;
import cga.framework.shape.Shape;

public class Test2Main extends Framework {

public MyShape shapes;

public Test2Main(int width, int height) {
super(width, height);
((RendererJ2d) this.renderer).addKeyListener(new MyKeyListener());
}

@Override
public void initGraph() {
shapes=new MyShape();
add(shapes);
}

public static void main(String args[]) {
Test2Main tm = new Test2Main(300, 300);
tm.start();
}

private class MyShape extends Shape{
ArrayList al = new ArrayList();
@Override
public void render(Matrix arg0, Camera arg1, Renderer arg2) {
synchronized (al) {
for(Line2d l : al)
{
l.render(arg0, arg1, arg2);
}
}
}
}
private class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
synchronized (shapes.al) {
shapes.al.add(new Line2d(0, 0, 5, 5));
}
}
}
}

```